假设我有一个数组String [],每个元素有47个字符。字符串的格式如下:
"My name is Fouad *6,#%$ , 67655-76 " // 30 characters for the name (It may contains special charaters) & ", " & 15 charaters for the phone-number
我希望将每个元素分成两部分:名称(删除之后的空格而不是之间的空格)&数字(也可以删除后面没有空格的空格)
所以我可以有两个数组String []:name []&数[]
我该怎么做这样的算法?
答案 0 :(得分:2)
答案 1 :(得分: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);
}
}
答案 2 :(得分:1)
只需使用以下代码
即可"My name is Fouad *6,#%$ , 67655-76 ".substring(0,30).trim()
"My name is Fouad *6,#%$ , 67655-76 ".substring(30).trim()