简体   繁体   English

在Java中将String转换为String数组

[英]converting a String into a String array in Java

I simply cannot find how to do this. 我根本找不到解决方法。 so, I have a String that will be something like this: 所以,我有一个字符串,将是这样的:

do this
then do that
then more of this

and I need to turn this into an array of strings. 我需要将其转换为字符串数组。 where every new line is needs to be a separate entry in the array. 每个新行都需要在数组中作为单独的条目。 I need this done so I can process every line or command separately. 我需要完成此操作,以便可以分别处理每行或命令。 I can't enter it directly into an array because this is loaded from a text document, and there is some code that removes comments and unnecessary empty lines, and a few more things with the string before it needs to become an array. 我无法将其直接输入数组,因为它是从文本文档中加载的,并且有一些代码可以删除注释和不必要的空行,并且在将字符串变成数组之前需要对字符串进行一些处理。

thanks in advance! 提前致谢!

String s = "do this\nthen do that\nthen more of this";
String[] split = s.split("\n");

尝试这个:

string.split("(?m)\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM