假设我有一个数组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我想把每个元素分成两部分: name (去掉后面的空格,而不是中间的空格)& number (去掉后面的空格,而不是中间的空格)
所以我可以有两个数组String[]:name[]和number[]
我怎么做这样的算法呢?
发布于 2011-05-07 04:19:10
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);
}
}发布于 2011-05-07 04:16:34
填充namei
发布于 2011-05-07 04:12:51
只需使用以下代码
"My name is Fouad *6,#%$ , 67655-76 ".substring(0,30).trim()
"My name is Fouad *6,#%$ , 67655-76 ".substring(30).trim()https://stackoverflow.com/questions/5916500
复制相似问题