• String常用方法解析


    package com.javaBase.string;
    
    import java.util.Locale;
    
    /**
     * 〈一句话功能简述〉;
     * 〈String类中常用的方法整理〉
     *
     * @author 
     * @see [相关类/方法](可选)
     * @since [产品/模块版本] (可选)
     */
    public class CommonStringMethod {
    
        public static void main(String[] args) {
    //        testLength();
    //        testChartAt();
    //        testToCharyArray();
    //        testIndexOf();
    //        testToUpperCaseAndToLowerCase();
    //        testSplit();
    //        testTrim();
    //        testSubstring();
    //        testEqualsIgnoreCase();
    //        testContains();
    //        testStartsWithAndEndsWith();
            testReplaceAll();
        }
    
        /**
         * length() 返回此字符串的长度。长度等于字符串中 Unicode 代码单元的数量。
         * 返回值为 int 类型。得到一个字符串的字符个数(中、英、空格、转义字符皆为字符,计入长度)
         */
        public static void testLength() {
            String str = "123456 	 
    ";
            System.out.println(str.length());
        }
    
        /**
         * charAt(index);返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,
         * 第二个位于索引 1 处,依此类推,这类似于数组索引。
         */
        public static void testChartAt() {
            String str = "123456";
            System.out.println(str.charAt(0));
            System.out.println(str.charAt(1));
        }
    
        /**
         * toCharArray();将此字符串转换为一个新的字符数组。
         */
        public static void testToCharyArray() {
            String str = "123456";
            char[] arr = str.toCharArray();
            for (int i = 0; i < arr.length; i++)
                System.out.print(arr[i]);
        }
    
        /**
         * indexOf(int ch);返回指定字符在此字符串中第一次出现处的索引
         * indexOf(int ch, int fromIndex);返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
         * indexOf(String str);返回指定子字符串在此字符串中第一次出现处的索引
         * indexOf(String str, int fromIndex);返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
         */
        public static void testIndexOf() {
            String str = "123456";
            System.out.print(str.indexOf('1'));
            System.out.print(str.indexOf('1',1));
            System.out.print(str.indexOf("1"));
            System.out.print(str.indexOf("1",1));
        }
    
        /**
         * toLowerCase();使用默认语言环境的规则将此 String 中的所有字符都转换为小写
         * toLowerCase(Locale locale);使用给定 Locale 的规则将此 String 中的所有字符都转换为小写
         * toUpperCase();使用默认语言环境的规则将此 String 中的所有字符都转换为大写
         * toUpperCase(Locale locale);使用给定 Locale 的规则将此 String 中的所有字符都转换为大写
         */
        public static void testToUpperCaseAndToLowerCase() {
            String str = "123456";
            System.out.println(str.toUpperCase());  //123456
            String str2 = "奥术大师多";
            System.out.println(str2.toUpperCase());  //奥术大师多
            String str3 = "abcA";
            System.out.println(str3.toUpperCase());
            System.out.println(str3.toUpperCase(Locale.CANADA_FRENCH)); //了解下local类
            System.out.println(str3.toLowerCase());
        }
    
        /**
         * split();根据给定正则表达式的匹配拆分此字符串。
         */
        public static void testSplit() {
            String str = "123456";
            String[] arr = str.split(",");
            for (int i=0;i<arr.length;i++)
                System.out.println(arr[i]);
        }
    
        /**
         * equals(Object anObject);将此字符串与指定的对象比较。当且仅当该参数不为 null,
         * 并且是与此对象表示相同字符序列的 String 对象时,结果才为 true
         */
        public static void testEquals() {
            String str = "123456";
            System.out.println("123".equals(str));
        }
    
        /**
         * trim();返回字符串的副本,忽略前导空白和尾部空白。
         * replace(char oldChar, char newChar) ;返回一个新的字符串,它是通过用 newChar
         * 替换此字符串中出现的所有 oldChar 得到的。
         */
        public static void testTrim() {
            String str = " 12 3456 ";
            System.out.println(str.trim());  //12 3456
            System.out.println(str.replace(" ","")); //123456
        }
    
        /**
         * substring(int beginIndex);返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,
         * 直到此字符串末尾。
         * substring(int beginIndex,int endIndex);返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的
         * beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
         */
        public static void testSubstring() {
            String str = "123456";
            System.out.println(str.substring(2));
            System.out.println(str.substring(2,4));  //左闭右开
        }
    
        /**
         * equalsIgnoreCase(String anotherString);将此 String 与另一个 String 比较,不考虑大小写
         */
        public static void testEqualsIgnoreCase() {
            String str = "abc";
            System.out.println("AbC".equalsIgnoreCase(str));
        }
    
        /**
         * contains(CharSequence s);当且仅当此字符串包含指定的 char 值序列时,返回 true。
         */
        public static void testContains() {
            String str = "abcdef123";
            System.out.println(str.contains("f12"));
        }
    
        /**
         * startsWith(String prefix);测试此字符串是否以指定的前缀开始。
         * endsWith(String suffix) ;测试此字符串是否以指定的后缀结束。
         */
        public static void testStartsWithAndEndsWith() {
            String str = "abcdef123";
            System.out.println(str.endsWith("123"));
            System.out.println(str.startsWith("123"));
        }
    
        /**
         * replaceAll(String regex, String replacement);使用给定的 replacement 替换此字符串
         * 所有匹配给定的正则表达式的子字符串。
         */
        public static void testReplaceAll() {
            String str = "abcdef1231";
            str = str.replaceAll("1","@");
            System.out.println(str);
            String str2 = "abcdef1231";
            str2 = str2.replace("1","#");
            System.out.println(str2);
            //一样?
            String str3 = "abcdef1231";
            str3 = str3.replaceAll("\d", "*"); //abcdef**** replaceAll()支持正则表达式
            System.out.println(str3);
        }
    }
  • 相关阅读:
    #import &lt;/usr/include/objc/objc-class.h&gt; not such file or directory问题的解决方法
    关于二进制补码
    DirectSound的应用
    Qt on Android: http下载与Json解析
    双绞线的制作,T568A线序,T568B线序
    Java设计模式之从[暗黑破坏神存档点]分析备忘录(Memento)模式
    linux 系统升级中的一些配置
    malloc函数具体解释
    老鸟的Python新手教程
    ubuntu12.04 安装配置jdk1.7
  • 原文地址:https://www.cnblogs.com/jxxblogs/p/11045901.html
Copyright © 2020-2023  润新知