java trim 逗号_java-从逗号分隔的字符串中删除结尾的逗号

java-从逗号分隔的字符串中删除结尾的逗号

我从具有多个逗号的数据库中获取了String(kushalhs, mayurvm, narendrabz)。 我想删除最后一个逗号,但实际上找不到一种简单的方法。

我拥有的:kushalhs, mayurvm, narendrabz

我想要的是:kushalhs, mayurvm, narendrabz

Kushal Shah asked 2020-07-24T13:01:07Z

16个解决方案

122 votes

要删除"a , b , c , "部分(紧随其后的是字符串结尾),您可以执行以下操作:

str = str.replaceAll(", $", "");

与需要特殊处理这种情况的"a , b , c , "/2980606182613844844993解决方案相反,这可以优雅地处理空列表(空字符串)。

示例代码:

String str = "kushalhs, mayurvm, narendrabz, ";

str = str.replaceAll(", $", "");

System.out.println(str); // prints "kushalhs, mayurvm, narendrabz"

注意:由于对"a , b , c , "部分有一些注释和建议的编辑:该表达式应与要删除的结尾部分匹配。

如果您的输入看起来像"a , b , c , ",请使用" , $"。

If your input looks like "a , b , c , ", use " , $".

If your input looks like "a , b , c , ", use " , $".

我认为你说对了。

aioobe answered 2020-07-24T13:03:40Z

10 votes

您可以使用此:

String abc = "kushalhs , mayurvm , narendrabz ,";

String a = abc.substring(0, abc.lastIndexOf(","));

srini answered 2020-07-24T13:04:00Z

8 votes

使用番石榴将所有逗号标准化。 将字符串用逗号分开,扔掉空容器,然后将它们重新连接在一起。 两个电话。 没有循环。 第一次工作:

import com.google.common.base.Joiner;

import com.google.common.base.Splitter;

public class TestClass {

Splitter splitter = Splitter.on(',').omitEmptyStrings().trimResults();

Joiner joiner = Joiner.on(',').skipNulls();

public String cleanUpCommas(String string) {

return joiner.join(splitter.split(string));

}

}

public class TestMain {

public static void main(String[] args) {

TestClass testClass = new TestClass();

System.out.println(testClass.cleanUpCommas("a,b,c,d,e"));

System.out.println(testClass.cleanUpCommas("a,b,c,d,e,,,,,"));

System.out.println(testClass.cleanUpCommas("a,b,,, ,c,d, ,,e,,,,,"));

System.out.println(testClass.cleanUpCommas("a,b,c,d, e,,,,,"));

System.out.println(testClass.cleanUpCommas(",,, ,,,,a,b,c,d, e,,,,,"));

}

}

输出:

a,b,c,d,e

a,b,c,d,e

a,b,c,d,e

a,b,c,d,e

a,b,c,d,e

我个人讨厌统计子字符串的限制以及所有的废话。

Steve J answered 2020-07-24T13:05:33Z

3 votes

对于多个逗号

String names = "Hello,World,,,";

System.out.println(names.replaceAll("(,)*$", ""));

输出:你好,世界

Pandiarajan answered 2020-07-24T13:05:57Z

2 votes

我在此线程上很晚,但希望它对某些人有所帮助。

String abc = "kushalhs , mayurvm , narendrabz ,";

if(abc.indexOf(",") != -1){

abc = abc.substring(0,abc.length() - 1);

}

Yogesh answered 2020-07-24T13:06:17Z

1 votes

此方法在BalusC的StringUtil类中。 他的博客

我经常使用它,并将修剪任何值的任何字符串:

/**

* Trim the given string with the given trim value.

* @param string The string to be trimmed.

* @param trim The value to trim the given string off.

* @return The trimmed string.

*/

public static String trim(String string, String trim) {

if (string == null) {

return null;

}

if (trim.length() == 0) {

return string;

}

int start = 0;

int end = string.length();

int length = trim.length();

while (start + length <= end && string.substring(

start, start + length).equals(trim)) {

start += length;

}

while (start + length <= end && string.substring(

end - length, end).equals(trim)) {

end -= length;

}

return string.substring(start, end);

}

例如:

trim("1, 2, 3, ", ", ");

epoch answered 2020-07-24T13:06:46Z

1 votes

(^(\s*?\,+)+\s?)|(^\s+)|(\s+$)|((\s*?\,+)+\s?$)

正则表达式

例如:

a, b, c

, ,a, b, c,

,a, b, c ,

,,a, b, c, ,,,

, a, b, c, ,

a, b, c

a, b, c ,,

, a, b, c,

, ,a, b, c, ,

, a, b, c ,

,,, a, b, c,,,

,,, ,,,a, b, c,,, ,,,

,,, ,,, a, b, c,,, ,,,

,,,a, b, c ,,,

,,,a, b, c,,,

a, b, c

变成:

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

a, b, c

IceWarrior353 answered 2020-07-24T13:07:14Z

1 votes

您可以执行类似使用String类的join函数的操作。

import java.util.Arrays;

import java.util.List;

public class Demo {

public static void main(String[] args) {

List items = Arrays.asList("Java", "Ruby", "Python", "C++");

String output = String.join(",", items);

System.out.println(output);

}

}

Rajesh Samson answered 2020-07-24T13:07:34Z

0 votes

检查是否str.charAt(str.length() -1) == ','。然后做str = str.substring(0, str.length()-1)

Saurabh answered 2020-07-24T13:07:54Z

0 votes

String str = "kushalhs , mayurvm , narendrabz ,";

System.out.println(str.replaceAll(",([^,]*)$", "$1"));

Arkadiusz Cieśliński answered 2020-07-24T13:08:10Z

0 votes

您可以尝试一下,它对我有用:

if (names.endsWith(",")) {

names = names.substring(0, names.length() - 1);

}

或者您也可以尝试以下方法:

string = string.replaceAll(", $", "");

Tarit Ray answered 2020-07-24T13:08:34Z

0 votes

public static String removeExtraCommas(String entry) {

if(entry==null)

return null;

String ret="";

entry=entry.replaceAll("\\s","");

String arr[]=entry.split(",");

boolean start=true;

for(String str:arr) {

if(!"".equalsIgnoreCase(str)) {

if(start) {

ret=str;

start=false;

}

else {

ret=ret+","+str;

}

}

}

return ret;

}

blathur answered 2020-07-24T13:08:49Z

0 votes

我正在使用正则表达式从我的项目共享代码,您可以执行此操作...

String ChildBelowList = "";

if (!Childbelow.isEmpty()) {

for (int iCB = 0; iCB < Childbelow.size(); iCB++) {

ChildBelowList = ChildBelowList += Childbelow.get(iCB) + ",";

}

ChildBelowList = ChildBelowList.replaceAll("(^(\\s*?\\,+)+\\s?)|(^\\s+)|(\\s+$)|((\\s*?\\,+)+\\s?$)", "");

tv_childbelow.setText(ChildBelowList);

} else {

ll_childbelow.setVisibility(View.GONE);

}

Hitesh Kushwah answered 2020-07-24T13:09:09Z

0 votes

还有一个...这也清除了包含空格的内部逗号:

从, , , ,one,,, , ,two three, , , ,,four, , , , ,到one,two three, four

text.replaceAll("^(,|\\s)*|(,|\\s)*$", "").replaceAll("(\\,\\s*)+", ",");

Christophe Roussy answered 2020-07-24T13:09:34Z

-1 votes

您可以使用“ Java 8”执行类似的操作

private static void appendNamesWithComma() {

List namesList = Arrays.asList("test1", "tester2", "testers3", "t4");

System.out.println(namesList.stream()

.collect(Collectors.joining(", ")));

}

Damien-Amen answered 2020-07-24T13:09:54Z

-1 votes

或类似这样的东西:

private static String myRemComa(String input) {

String[] exploded = input.split(",");

input="";

boolean start = true;

for(String str : exploded) {

str=str.trim();

if (str.length()>0) {

if (start) {

input = str;

start = false;

} else {

input = input + "," + str;

}

}

}

return input;

}

crezes answered 2020-07-24T13:10:13Z

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值