在使用 myString.split(“\n”) 时遇到问题;
我需要将输入字符串拆分为多个部分。拆分应发生在“\n”处(字面意思是反斜杠-n,而不是换行符)。例如,我想转动这个:
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
进入这个:
x = [2,0,5,5]
y = [0,2,4,4]
draw y #0000ff
y = y & x
draw y #ff0000
我本来以为这就足够了。stringArray = string.split("\n");
但它为我提供的输出与以下代码中的输入相同:
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Input\n");
String s = br.readLine();
NewInterpreter interpreter = new NewInterpreter(s);
interpreter.run();
}
public NewInterpreter(String input) {
this.input = input;
this.index = 0;
this.inputComponents = input.split("\n");
System.out.println("Output: ");
for(String s : inputComponents)
System.out.println(s);
}
Enter Input
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000"
Output:
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
任何帮助都非常感谢,谢谢!