• Java 字符串类型常用方法


    常用方法

    获取字符串长度

    public int length()

    字符串Unicode操作

    这部分用的不多,不是很清楚,先记载在这。

    //获取指定索引处的元素对应的unciode编码
    public int codePointAt(int index)
    //获取指定索引处之前的元素对应的unciode编码
    public int codePointBefore(int index)
    //获取指定的开始索引到结束索引之间元素的unciode编码的个数
    public int codePointCount(int beginIndex, int endIndex)
    //获取指定索引处的元素开始偏移codePointOffset的索引
    public int offsetByCodePoints(int index, int codePointOffset)

    字符串字符操作

    //获取字符串指定索引处的字符
    public char charAt(int index)
    //获取字符串中元素对应的字符数组
    void getChars(char dst[], int dstBegin)
    //获取字符串中部分元素对应的字符数组
    public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
    //获取字符串对应的字符数组
    public char[] toCharArray()

    字符串字节操作

    public byte[] getBytes(String charsetName)
    public byte[] getBytes(Charset charset)
    public byte[] getBytes()

    字符串比较操作

    (1)比较是否相等

    比较两个字符串是否相等,anObject 必须为字符串,否则返回 false。

    public boolean equals(Object anObject)

    比较两个字符串是否相等,忽略大小写。

    public boolean equalsIgnoreCase(String anotherString)

    这两个方法可用于比较String与StringBuilder,StringBuffer是否相等。

    String、StringBuilder、StringBuffer 都实现了 CharSequence 接口,StringBuilder是非线程安全的,而StringBuffer是线程安全的。

    public boolean contentEquals(StringBuffer sb)
    public boolean contentEquals(CharSequence cs)

    (2)比较大小

    字符串比较大小。

    按字符串中字符的字典顺序(也就是字符对应的Unicode值)比较两个字符串,返回 thisString - anotherString 的差值。

    thisString > anotherString,返回值大于0

    thisString = anotherString,返回值等于0

    thisString > anotherString,返回值小于0

    public int compareTo(String anotherString)

    字符串比较大小,忽略大小写。比较方法和上面相同。

    public int compareToIgnoreCase(String str)

    (3)部分比较是否相等

    当某个字符串调用该方法时,表示从当前字符串的 toffset 位置开始,取一个长度为 len 的子串,然后从另一个字符串 other 的 ooffset 位置开始也取一个长度为 len 的子串,然后比较这两个子串是否相同,如果这两个子串相同则返回 true,否则返回 false。

    public boolean regionMatches(int toffset, String other, int ooffset, int len)

    比上面的方法多了一个 boolean 类型的 ignoreCase 参数,用来确定比较时是否忽略大小写,当 ignoreCase 为 true 表示忽略大小写。为 false 时和上面方法就相同了。

    public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

    字符串测试操作

    (1)开头测试

    字符串的指定索引处是否以指定的一个子字符串开头。

    public boolean startsWith(String prefix, int toffset)

    字符串是否以指定的一个子字符串开头。

    public boolean startsWith(String prefix)

    (2)结尾测试

    字符串是否以指定的一个子字符串结尾

    public boolean endsWith(String suffix)

    (3)包含测试

    字符串是否包含指定的字符序列。

    public boolean contains(CharSequence s)

    (4)匹配测试

    字符串是否能匹配指定的正则表达式

    public boolean matches(String regex)

    (5)为空测试

    该方法其实是判断字符串的长度是否为0,所以无法判断含有空格的空串。

    public boolean isEmpty()

    字符串索引操作

    返回在此字符串中第一次出现的指定子串 str 的索引。没有则返回 -1

    从指定的索引 fromIndex 处开始向后搜索(包括该索引),返回在此字符串中第一次出现的指定子字符串 str 的索引。没有则返回-1。

    如果是 int 型参数 ch,则代表的是子串对应的 Unicode 值。

    public int indexOf(int ch)
    public int indexOf(int ch, int fromIndex)
    public int indexOf(String str)
    public int indexOf(String str, int fromIndex)

    返回在此字符串中最后一次出现的指定的子字符串 str 的索引。

    从指定的索引 fromIndex 处开始向前搜索(包括该索引),返回在此字符串中最后一次出现的指定子字符串 str 的索引。没有则返回-1。

    如果是 int 型参数 ch,则代表的是子串对应的 Unicode 值。

    public int lastIndexOf(int ch)
    public int lastIndexOf(int ch, int fromIndex)
    public int lastIndexOf(String str)
    public int lastIndexOf(String str, int fromIndex)

    字符串修改操作

    (1)截取

    指定开始索引(包含)和结束索引(不包含),截取字符串。

    subSequence 和 substring 一样,源码里直接调用的 substring。

    public String substring(int beginIndex)
    public String substring(int beginIndex, int endIndex)
    public CharSequence subSequence(int beginIndex, int endIndex)

    (2)拼接

    会将 str 拼接到源字符串后边,返回一个新的字符串。

    public String concat(String str)

    (3)替换

    将指定字符 oldChar 或字符序列 target 全部替换为新的字符 newChar 或者新的字符序列 replacement。

    public String replace(char oldChar, char newChar)
    public String replace(CharSequence target, CharSequence replacement)

    只会替换第一次出现的匹配正则表达式 regex 的子串。

    public String replaceFirst(String regex, String replacement)

    会替换所有匹配正则表达式 regex 的子串。

    public String replaceAll(String regex, String replacement)

    (4)分割

    以能匹配正则表达式 regex 的分割符进行分割,分割成指定元素个数 limit 的数组。

    public String[] split(String regex, int limit)
    public String[] split(String regex)

    (5)转换大小写

    转换为小写,可以指定语言环境,一般直接用默认的语言环境,也就是不指定。

    public String toLowerCase(Locale locale)
    public String toLowerCase()

    转换为大写

    public String toUpperCase(Locale locale)
    public String toUpperCase()

    (6)去空格

    注意:该方法只会去除字符串前后的空格,中间的去不掉。

    public String trim()

    静态方法

    join拼接新字符串

    用指定分隔符 delimiter 分割数组或集合的元素,拼接成新的字符串。

    public static String join(CharSequence delimiter, CharSequence... elements)
    public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

    示例:

    System.out.println(String.join(":", "aa", "bb", "cc")); // aa:bb:cc
    List<String> list = new ArrayList<>();
    list.add("hello");
    list.add("world");
    list.add("wang");
    list.add("bo");
    System.out.println(String.join(" ", list)); //hello world wang bo

    format格式化字符串

    将对象数组按指定格式转换为字符串。

    参考文档:http://www.cnblogs.com/fsjohnhuang/p/4094777.html

    public static String format(String format, Object... args)
    public static String format(Locale l, String format, Object... args)

    valueOf转换字符串

    将各种数据类型转换为字符串

    public static String valueOf(Object obj)
    public static String valueOf(char data[])
    public static String valueOf(char data[], int offset, int count)
    public static String valueOf(boolean b)
    public static String valueOf(char c)
    public static String valueOf(int i)
    public static String valueOf(long l)
    public static String valueOf(float f)
    public static String valueOf(double d)

    copyValueOf拼接字符串

    将字符数组中的元素拼接为字符串,可以指定开始索引 offset 和元素总数 count。

    public static String copyValueOf(char data[], int offset, int count)
    public static String copyValueOf(char data[])

    示例:

    char[] array = new char[]{'a','b','c','d'};
    System.out.println(String.copyValueOf(array));//abcd
    System.out.println(String.copyValueOf(array, 2, 1));//c

    工具方法

    获取子字符串出现的次数

    /**
     * 获取字符串中子字符串出现的次数
     * @param srcStr
     * @param str
     */
    public static void strNum(String srcStr, String str){
        int number = 0;
        for (int i = 0; i < srcStr.length(); i++) {
            if (srcStr.regionMatches(i, str, 0, str.length())) {
                number ++;
            }
        }
        System.out.println("number=" + number);
    }

    获取子字符串的所有索引

    (1)递归实现

    /**
     * 获取字符串中所有子字符串的索引
     * @param srcStr
     * @param str
     * @param i
     */
    public static void strIndex(String srcStr, String str, int i){
        int index = srcStr.indexOf(str, i);
        if (index != -1) {
            System.out.println("index=" + index);
            strIndex(srcStr, str, index + str.length());
        }
    }

    (2)while循环实现

    /**
     * 获取字符串中所有子字符串的索引
     * @param srcStr
     * @param str
     * @param i
     */
    public static void strIndex(String srcStr, String str, int i){
        while (true) {
            int index = srcStr.indexOf(str, i);
            if (index == -1) {
                return;
            }
            System.out.println("index=" + index);
            i = index + str.length();
        }
    }
  • 相关阅读:
    no,no,不要使用kill -9
    Linux中etc目录详解
    Quartz任务调度器的使用
    RMI(Remote Method invocation,远程方法访问)
    SilverLight扩展控件RadTreeView
    SiverLight和HTML交互
    SilverLight控件之ContextMenu和RadContextMenu(菜单)
    SilverLight之向后台请求数据-WebClient
    SilverLight控件样式及控件模版
    在SilverLight中代码编写可选择树节点
  • 原文地址:https://www.cnblogs.com/wbxk/p/6933851.html
Copyright © 2020-2023  润新知