简体   繁体   English

从字符串中删除前导空格

[英]Removing leading spaces from a string

I have a String like follows 我有一个如下的字符串

 abc, def tgy, awed djdj

Now i want to get all the parts of this string seperated by comma. 现在,我想用逗号分隔此字符串的所有部分。 Best way i can image is doing the following 我可以成像的最佳方法是执行以下操作

final String[] splits = country.split(",");

Now i should have a array with 3 strings. 现在我应该有3个字符串的数组。 But i dont want the leading spaces in each string also. 但是我也不想每个字符串中的前导空格。 How can i remove them. 我如何删除它们。 Is there a special way to do that? 有一种特殊的方法吗? Or i have to use the same old regex way to remove the leading spaces. 或者我必须使用相同的旧正则表达式方法来删除前导空格。

您可以使用此正则表达式从拆分元素中删除前导/后缀空格:

final String[] splits = country.trim().split(" *, *");

在字符串上使用此调用可删除前导和尾随空格:

arbitraryVariable = arbitraryVariable.trim()
,\s*

You can split by this as well.This will remove leading spaces.See demo. 您也可以按此拆分。这将删除前导空格。请参见演示。

http://regex101.com/r/jT3pG3/13 http://regex101.com/r/jT3pG3/13

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

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