-2

单个字符串的输出是

iPad 触控笔和圆珠笔
通常在 24 小时内发货
$9.99

我想将上述字符串的每一行保存到单独的字符串中,例如:

String one = "iPad Stylus & Ballpoint Pen";  
String two = "Usually ships in 24 hrs";  
String three = "$9.99";    

应该使用什么方法?

4

2 回答 2

2
String inputString = " iPad Stylus & Ballpoint Pen    
     Usually ships in 24 hrs    
    $9.99";



String lines[] = inputString.split("\\r?\\n");
于 2012-06-07T14:52:17.533 回答
0

使用番石榴

Splitter splitter = Splitter.on('\n').trimResults().omitEmptyStrings();
Iterable<String> items = splitter.split(text);

参见Guava Explained > Strings > Splitter

于 2012-06-07T14:54:22.000 回答