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

java \u600e\u4e48\u53bb\u6389string \u7b2c\u4e00\u4e2a\u5b57\u7b26

String str1 = "abcdefg";String str2 = str1.substring(1, str1.length());str2\u663e\u793a\u7ed3\u679c bcdefg

\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

i、replace方法

    该方法的作用是替换字符串中所有指定的字符,然后生成一个新的字符串。经过该方法调用以后,原来的字符串不发生改变。例如:

    String s = “abcat”;
    String s1 = s.replace(‘a’,‘1’);

    该代码的作用是将字符串s中所有的字符a替换成字符1,生成的新字符串s1的值是“1bc1t”,而字符串s的内容不发生改变。

    如果需要将字符串中某个指定的字符串替换为其它字符串,则可以使用replaceAll方法,例如:

    String s = “abatbac”;
    String s1 = s.replaceAll(“ba”,“12”);

    该代码的作用是将字符串s中所有的字符串“ab”替换为“12”,生成新的字符串“a12t12c”,而字符串s的内容也不发生改变。

    如果只需要替换第一个出现的指定字符串时,可以使用replaceFirst方法,例如:

    String s = “abatbac”;
    String s1 = s. replaceFirst (“ba”,“12”);

    该代码的作用是只将字符串s中第一次出现的字符串“ab”替换为字符串“12”,则字符串s1的值是“a12tbac”。



String API中有replace方法 。

扩展阅读:java windowbuilder ... javascript string ... javascript入门 ... javastring转int的方法 ... 为什么都不建议java转测试 ... java switch string ... println ... string强制转换int ... java中append ...

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