0

Suppose I have an array String[] which has 47 characters in each element. The format of the strings is as the following:

"My name is Fouad *6,#%$       , 67655-76       " // 30 characters for the name (It may contains special charaters) & ", " & 15 charaters for the phone-number

and I want to split each element into two parts: name (with removing the spaces after it not the spaces in between) & number (also with removing the spaces after it not the spaces in between)

so I can have two arrays String[]: name[] & number[]

How can I do such algorithm ?

3 Answers 3

2
public class Main
{
    public static void main (String [] args)
    {
        String [] array = new String []
        {
                "My name is Fouad *6,#%$       , 67655-76       ",
                "My name is Fouad *7,#%$       , 67655-77       ",
                "My name is Fouad *8,#%$       , 67655-78       ",
                "My name is Fouad *9,#%$       , 67655-79       ",
                "My name is Fouad *0,#%$       , 67655-70       ",
                "My name is Fouad *1,#%$       , 67655-71       "
        };

        String [] names = new String [array.length];
        String [] phones = new String [array.length];

        for (int i = 0; i < array.length; i++)
        {
            names [i] = array [i].substring (0, 30).trim ();
            phones [i] = array [i].substring (31).trim ();
        }

        for (String name : names) System.out.println (name);
        for (String phone : phones) System.out.println (phone);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

but .trim() will remove the spaces in between ? it will be like ""MynameisFouad"
No it does not. trim() trims the beginning and the end of a string of whitespace characters.
2
  • Create name[] and number[] with the same length as your original array
  • Loop through the array
    1. fill name[i] with array[i].substring(0, 30).trim()
    2. fill number[i] with array[i].substring(30).trim()

Comments

1

Just use following code

"My name is Fouad *6,#%$       , 67655-76       ".substring(0,30).trim()
"My name is Fouad *6,#%$       , 67655-76       ".substring(30).trim()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.