gpt4 book ai didi

java - 安卓 : How to delete after so many characters?

转载 作者:行者123 更新时间:2023-12-01 15:26:15 27 4
gpt4 key购买 nike

我需要在这么多之后删除 coma(,) 。假设有一个带有 4 个逗号的字符串“i,am,ok,today,hello”,我想在 2 个comans 之后删除重置,但保留文本,只需在前 2 个comans 之后删除comans?

最佳答案

循环字符串,使用 StringBuilder 重建并检查逗号:

static String stripCommasAfterN(String s, int n)
{
StringBuilder builder = new StringBuilder(s.length());
int commas = 0;

for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);

if (c == ',')
{
if (commas < n)
{
builder.append(c);
}
commas++;
}
else
{
builder.append(c);
}
}

return builder.toString();
}

关于java - 安卓 : How to delete after so many characters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098676/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com