Java中几个字符串的操作函数总结

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 07:38   11   0

1. String.replace()

功能:以新字符串(或单个字符)替换旧的字符串(或单个字符)

用法:

举例:

class Solution {
    public static void main(String []args){
     String str="abbcd";
     System.out.println("原字符串"+str);
     String str2=str.replace('b', '#');
     System.out.println("替换后的字符串"+str2);
    }
}
输出:
原字符串abbcd
替换后的字符串a##cd



class Solution{
    public static void main(String []args){
     String str="abbcd";
     System.out.println("原字符串"+str);
     String str2=str.replace("bb", "$$");
     System.out.println("替换后的字符串"+str2);
    }
}
输出:
原字符串abbcd
替换后的字符串a$$cd

源码:

  public String replace(CharSequence target, CharSequence replacement) {
        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
    }

2.String.replaceAll()

功能:用正则表达式的方法替换原字符串

用法:

举例:

class Solution {
    public static void main(String []args){
     String str="abbcbd";
     System.out.println("原字符串"+str);
     String str2=str.replaceAll("b+","#");//匹配一个及以上b字符序列
     System.out.println("替换后的字符串"+str2);
    }
}
输出:
原字符串abbcbd
替换后的字符串a#c#d

源码:

public String replaceAll(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }

    /**
     * Replaces each substring of this string that matches the literal target
     * sequence with the specified literal replacement sequence. The
     * replacement proceeds from the beginning of the string to the end, for
     * example, replacing "aa" with "b" in the string "aaa" will result in
     * "ba" rather than "ab".

3.String.split()

功能:Java中的 split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回;

用法:最常用的一种用法,传参为字符串或正则表达式,点击到达参考文章

举例:

String str="1234@abc";
String[] a = str.split("@");
System.out.println("处理结果: "+a[0]+","+a[1]);   //输出的是: 处理结果: 1234,abc

源码:

 public String[] split(String regex) {
        return split(regex, 0);
    }

    /**
     * Returns a new String composed of copies of the
     * {@code CharSequence elements} joined together with a copy of
     * the specified {@code delimiter}.

4.String.trim()

功能:trim() 方法用于删除字符串的头尾空白符。

用法:来自菜鸟教程

举例:

public class Test {
    public static void main(String args[]) {
        String Str = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( Str );

        System.out.print("删除头尾空白 :" );
        System.out.println( Str.trim() );
    }
}

源码:

* @return  A string whose value is this string, with any leading and trailing white
     *          space removed, or this string if it has no leading or
     *          trailing white space.
     */
    public String trim() {
        int len = value.length;
        int st = 0;
        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[st] <= ' ')) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= ' ')) {
            len--;
        }
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }

5.StringBuilder.append()

功能:在StringBuilder类型的可变长字符串对象后添加字符串(或其他对象)

用法:

举例:

class Solution {
    public static void main(String []args){
     StringBuilder str=new StringBuilder("");
     System.out.println("加入字符串: "+str.append("我是新加入的"));
     System.out.println("加入基本类型: "+str.append(134));
    }
}
输出:
加入字符串: 我是新加入的
加入基本类型: 我是新加入的134

源码:

  @Override
    public StringBuilder append(Object obj) {
        return append(String.valueOf(obj));
    }

    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

    /**
     * Appends the specified {@code StringBuffer} to this sequence.

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP