これはあなたが探しているものですか?
String text = "John|123 Wood Road|Street, London|25";
int first = text.indexOf("|");
int last = text.lastIndexOf("|");
String name = text.substring(0, first);
String age = text.substring(last + 1);
String address = text.substring(first + 1, last);
System.out.println(name);
System.out.println(address);
System.out.println(age);
出力:
John
123 Wood Road|Street, London
25
より一般的な解決策:
public static void main(String[] args)
{
String text = "John|123 Wood Road|Street, London|25";
for(String s : getArray(text, 0, 1, 0)) System.out.println(s);
}
public static String[] getArray(String text, int... pipeCount)
{
String[] arr = text.split("\\|");
String[] result = new String[3];
int counter = 0;
for(int i = 0; i < result.length; i++)
{
result[i] = "";
for(int j = 0; j <= pipeCount[i]; j++) result[i] += arr[counter++];
}
return result;
}
出力:
John
123 Wood Road|Street, London
25