在Java中删除部分字符串

在Java中删除部分字符串,第1张

在Java中删除部分字符串

有多种方法可以做到这一点。如果您有要替换的字符串,则可以使用该类的

replace
replaceAll
方法
String
。如果您要替换子字符串,则可以使用
substring
API
获得该子字符串。

例如

String str = "manchester united (with nice players)";System.out.println(str.replace("(with nice players)", ""));int index = str.indexOf("(");System.out.println(str.substring(0, index));

要替换“()”中的内容,可以使用:

int startIndex = str.indexOf("(");int endIndex = str.indexOf(")");String replacement = "I AM JUST A REPLACEMENT";String toBeReplaced = str.substring(startIndex + 1, endIndex);System.out.println(str.replace(toBeReplaced, replacement));


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5478032.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存