JAVA String 如何去掉指定字符 JAVA String 如何去掉指定字符

JAVA String \u5982\u4f55\u53bb\u6389\u6307\u5b9a\u5b57\u7b26

i\u3001replace\u65b9\u6cd5
\u8be5\u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u6307\u5b9a\u7684\u5b57\u7b26\uff0c\u7136\u540e\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u7ecf\u8fc7\u8be5\u65b9\u6cd5\u8c03\u7528\u4ee5\u540e\uff0c\u539f\u6765\u7684\u5b57\u7b26\u4e32\u4e0d\u53d1\u751f\u6539\u53d8\u3002\u4f8b\u5982\uff1a

String s = \u201cabcat\u201d\uff1b String s1 = s.replace\uff08\u2018a\u2019\uff0c\u20181\u2019\uff09\uff1b \u8be5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u5c06\u5b57\u7b26\u4e32s\u4e2d\u6240\u6709\u7684\u5b57\u7b26a\u66ff\u6362\u6210\u5b57\u7b261\uff0c\u751f\u6210\u7684\u65b0\u5b57\u7b26\u4e32s1\u7684\u503c\u662f\u201c1bc1t\u201d\uff0c\u800c\u5b57\u7b26\u4e32s\u7684\u5185\u5bb9\u4e0d\u53d1\u751f\u6539\u53d8\u3002
\u5982\u679c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e2a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u5176\u5b83\u5b57\u7b26\u4e32\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528replaceAll\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a
String s = \u201cabatbac\u201d\uff1b String s1 = s.replaceAll\uff08\u201cba\u201d\uff0c\u201c12\u201d\uff09\uff1b \u8be5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u5c06\u5b57\u7b26\u4e32s\u4e2d\u6240\u6709\u7684\u5b57\u7b26\u4e32\u201cab\u201d\u66ff\u6362\u4e3a\u201c12\u201d\uff0c\u751f\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u201ca12t12c\u201d\uff0c\u800c\u5b57\u7b26\u4e32s\u7684\u5185\u5bb9\u4e5f\u4e0d\u53d1\u751f\u6539\u53d8\u3002
\u5982\u679c\u53ea\u9700\u8981\u66ff\u6362\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528replaceFirst\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a
String s = \u201cabatbac\u201d\uff1b String s1 = s. replaceFirst \uff08\u201cba\u201d\uff0c\u201c12\u201d\uff09\uff1b \u8be5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u53ea\u5c06\u5b57\u7b26\u4e32s\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u5b57\u7b26\u4e32\u201cab\u201d\u66ff\u6362\u4e3a\u5b57\u7b26\u4e32\u201c12\u201d\uff0c\u5219\u5b57\u7b26\u4e32s1\u7684\u503c\u662f\u201ca12tbac\u201d\u3002

\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1astring.replace("substring","");\u5c31\u662f\u5c06\u5b50\u4e32\u6362\u6210\u7a7a\u503c


\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a
\u5927\u4f53\u601d\u60f3\u5148\u8bf4\u4e0b\uff0c\u9996\u5148\uff0c\u4f60\u9700\u8981\u67e5\u627e\u7236\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u5b50\u4e32\uff0c\u5982\u679c\u5b58\u5728\u5c31\u627e\u5230\u8d77\u59cb\u548c\u7ed3\u675f\u7684\u5750\u6807\uff0c\u7136\u540e\u901a\u8fc7\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6765\u5f97\u5230\u65b0\u7684\u5b57\u7b26\u4e32

\u4ee5\u4e0b\u662f\u7528indexOf()\u67e5\u627e\u5b50\u4e32\u7684\u65b9\u6cd5\u548c\u7528substring()\u6765\u622a\u53d6\u5b50\u4e32\u7684\u65b9\u6cd5

Java\u4e2d\u5b57\u7b26\u4e32\u4e2d\u5b50\u4e32\u7684\u67e5\u627e\u5171\u6709\u56db\u79cd\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a
1\u3001int indexOf(String str) \uff1a\u8fd4\u56de\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u5728\u6b64\u5b57\u7b26\u4e32\u4e2d\u7684\u7d22\u5f15\u3002
2\u3001int indexOf(String str, int startIndex)\uff1a\u4ece\u6307\u5b9a\u7684\u7d22\u5f15\u5904\u5f00\u59cb\uff0c\u8fd4\u56de\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u5728\u6b64\u5b57\u7b26\u4e32\u4e2d\u7684\u7d22\u5f15\u3002
3\u3001int lastIndexOf(String str) \uff1a\u8fd4\u56de\u5728\u6b64\u5b57\u7b26\u4e32\u4e2d\u6700\u53f3\u8fb9\u51fa\u73b0\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u3002
4\u3001int lastIndexOf(String str, int startIndex) \uff1a\u4ece\u6307\u5b9a\u7684\u7d22\u5f15\u5904\u5f00\u59cb\u5411\u540e\u641c\u7d22\uff0c\u8fd4\u56de\u5728\u6b64\u5b57\u7b26\u4e32\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u3002

L=N.substring(0,i)

\u4e24\u79cd\u65b9\u6cd5\u7528\u4e8e\u4e0d\u540c\u60c5\u51b5\u4e0b\uff0c\u4f60\u9009\u62e9\u4f7f\u7528\u5427
\u5e0c\u671b\u53ef\u4ee5\u5e2e\u5230\u4f60

用replaceAll方法将需要去掉的部分替换成空字符串就可以了

replaceAll
public String replaceAll(String regex,
String replacement)

使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
调用此方法的 str.replaceAll(regex,
repl) 形式与以下表达式产生的结果完全相同:
Pattern.compile(regex).matcher(str).replaceAll(repl)

注意,在替代字符串中使用反斜杠 (\) 和美元符号 ($) 与将其视为字面值替代字符串所得的结果可能不同;请参阅 Matcher.replaceAll。如有需要,可使用
Matcher.quoteReplacement(java.lang.String)
取消这些字符的特殊含义。

参数:
regex - 用来匹配此字符串的正则表达式
replacement - 用来替换每个匹配项的字符串
返回:
所得 String
抛出:
PatternSyntaxException
- 如果正则表达式的语法无效
从以下版本开始:
1.4
另请参见:
Pattern

第一种方法:string.replace("substring","");就是将子串换成空值

第二种方法:
大体思想先说下,首先,你需要查找父串中是否存在子串,如果存在就找到起始和结束的坐标,然后通过截取字符串的方法来得到新的字符串

以下是用indexOf()查找子串的方法和用substring()来截取子串的方法

Java中字符串中子串的查找共有四种方法,如下:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

L=N.substring(0,i)

两种方法用于不同情况下,你选择使用吧
希望可以帮到你

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网