Java - 一次拆分和修剪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41953388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:48:53  来源:igfitidea点击:

Java - Split and trim in one shot

javastringsplit

提问by YCF_L

I have a String like this : String attributes = " foo boo, faa baa, fii bii,"I want to get a result like this :

我有一个这样的字符串:String attributes = " foo boo, faa baa, fii bii,"我想得到这样的结果:

String[] result = {"foo boo", "faa baa", "fii bii"};

So my issue is how should to make split and trim in one shot i already split:

所以我的问题是如何在我已经拆分的一个镜头中进行拆分和修剪:

String[] result = attributes.split(",");

But the spacesstill in the result :

spaces仍然在结果中:

String[] result = {" foo boo", " faa baa", " fii bii"};
                    ^           ^           ^

I know that we can make a loop and make trimfor every one but I want to makes it in shot.

我知道我们可以制作一个循环并trim为每个人制作,但我想在镜头中制作。

采纳答案by Raman Sahasi

Use regular expression \s*,\s*for splitting.

使用正则表达式\s*,\s*进行拆分。

String result[] = attributes.split("\s*,\s*");


For Initial and Trailing Whitespaces
The previous solution still leaves initial and trailing white-spaces. So if we're expecting any of them, then we can use the following solution to remove the same:

对于初始和尾随空格
先前的解决方案仍然保留初始和尾随空格。因此,如果我们期待它们中的任何一个,那么我们可以使用以下解决方案来删除它们:

String result[] = attributes.trim().split("\s*,\s*");

回答by ΦXoc? ? Пepeúpa ツ

What about spliting with comma and space:

用逗号和空格分割怎么样:

String result[] = attributes.split(",\s");

回答by Andrei Olar

Using java 8 you can do it like this in one line

使用 java 8 你可以在一行中做到这一点

String[] result = Arrays.stream(attributes.split(",")).map(String::trim).toArray(String[]::new);

回答by Maxple

If there is no text between the commas, the following expression will not create empty elements:

如果逗号之间没有文本,则以下表达式不会创建空元素:

String result[] = attributes.trim().split("\s*,+\s*,*\s*");

回答by Dmitriy Pichugin

String result[] = attributes.trim().split("\s*,[,\s]*");

previously posted here: https://blog.oio.de/2012/08/23/split-comma-separated-strings-in-java/

以前发布在这里:https: //blog.oio.de/2012/08/23/split-comma-separated-strings-in-java/

回答by TimeTrax

create your own custom function

创建您自己的自定义函数

private static String[] split_and_trim_in_one_shot(String string){
 String[] result  = string.split(",");
 int array_length = result.length;

 for(int i =0; i < array_length ; i++){
  result[i]=result[i].trim();
 }
 return result;

Overload with a consideration for custom delimiter

考虑自定义分隔符的重载

private static String[] split_and_trim_in_one_shot(String string, String delimiter){
 String[] result  = string.split(delimiter);
 int array_length = result.length;

 for(int i =0; i < array_length ; i++){
  result[i]=result[i].trim();
 }
 return result;