所以对于这个问题,我是用Java做的。这是我到目前为止所拥有的。谢谢!
package stringparser;
import java.util.Scanner;
public class StringParser
{
public static void main(String[] args)
{
String Name = "Billy Bob Joe";
String first = Name.substring(0,5);
String middle = Name.substring(6,12);
String last = Name.substring(13,16);
System.out.println("First name: " + first);
System.out.println("Middle name: " + middle);
System.out.println("Last name: " + last);
Scanner in = new Scanner(System.in);
System.out.print("Type any 3 names: ");
System.out.print("First name: ");
String a = in.nextLine();
System.out.print("Second name: ");
String b = in.nextLine();
System.out.print("Third name: ");
String c = in.next();
}
}
答案 0 :(得分:0)
我解释这个问题的两种方式。
总而言之,我发现使用String.indexOf
最快的方式,但不一定是最好的方式。
public StringParser () {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.println(name.replace(" ", "\n")); // replacing all spaces with new line characters
}
答案 1 :(得分:0)
此程序将使用空格分割字符串并将所有字符串打印为结果。您可以根据需要编辑for循环条件。希望它有所帮助:)
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] names = str.split(" ");
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
}