zoukankan      html  css  js  c++  java
  • String

    char charAt(int index)  

    return the value at the index     

    String s = "Hello world";
    System.out.println(s.charAt(0));    //H
    s = "中国";
    System.out.println(s.charAt(1));   //

    int compareTo(String other)  

    returns a negative value if the string comes before other in dictionary order, a positive

    value if the string comes after the other, or 0 if the strings are equal

                String s1 = new String("a");
            String s2 = new String("a");
            // compare the content instead of address
            System.out.println(s1.compareTo(s2));    // 0, 
            String s3 = new String("b");
            System.out.println(s1.compareTo(s3));    //-1    
    View Code

    boolean equals(Object other)

    return true if string equals other, only true when other is a string and the content is equals string

    boolean equalsIgnoreCase(String other)

    return true if the string equals other,except the upper/lowercase distinction

    boolean startsWith(String prefix)

    boolean endsWith(String suffix)

    return true if the string starts/ ends with prefix/suffix

    int indexOf(String str)

    int indexOf(String str, int fromIndex)

    return the start_index of the fisrt substring equals to the string str, or -1 if str not in this string,

    boolean contains(CharSequence str)

    Returns true if and only if this string contains the specified str

    String s1 = new String("_hello world hello java");
    String s2 = "hello";
    int index = s1.indexOf(s2);  // 1
    int nextIndex = s1.indexOf(s2, index+1);  //13
    View Code

    int lastIndexof(str, int fromIndex)

                   String s1 = new String("_hello world hello java");
            String s2 = "hello";
            int index = s1.lastIndexOf(s2);  // 13
            int nextIndex = s1.lastIndexOf(s2, index - 1 );  //1            
    View Code

    int lengh()

    String substring(int beginIndex, int endIndex)

    String toLowerCase()

    String toUpperCase()

    String replace(CharSequence oldString, CharSequence newString)    //just take the CharSequence as String

    String trim()    // eliminate all leading and tailing whitespace

                    String s1 = new String("Abc");
            String upper = s1.toUpperCase();  // ABC
            String lower = s1.toLowerCase();  // abc
            int length = s1.length();   // 3
            String newString = s1.replace("b", "2");  // A2c
            
            String s2 = "   haha    ";
            String trim = s2.trim();   // haha
            String subStr = s1.substring(0, 2);  // Ab [0,2)        
    View Code

    String join(CharSequence delimiter, CharSequence...elements)  // static

    return a new string joining all elements with the given delimiter

    char[] toCharArray()

    Converts this string to a new character array

    String s1 = new String("中国");
    String s2 = String.join("#", "a","b", "c"); //  a#b#c
    char[] arr =  s1.toCharArray();    // [中, 国]
    View Code
    
    

    static String format(String format, Object... args)

    Returns a formatted string using the specified format string and arguments.

    Format String Syntax google

    all the method above return the new String, cause the String is unmutable

    String replaceAll (String regex, String replacement)     // regex 正则语法

    String[] split(String regex)

    Splits this string around matches of the given regular expression.

    boolean matches(String regex)

    Tells whether or not this string matches the given regular expression.

                    String regex = "hello";
            String s = "hello jime sshello mm hellosdfs";
            String s2 = s.replaceAll(regex, "H");  // H jime ssH mm Hsdfs
            
            String s3 = "192.168.0.21";
            regex = "\.";
            String[] strArr = s3.split(regex);  // [192, 168, 0, 21]
    
            s3 = "abc@xxx.com";
            regex = "[\w]+@[\w]+\.com";  
            System.out.println(s3.matches(regex));   // true        
    View Code

    static String valueOf(int i);    // also can be float double byte...

    Returns the string representation of the int argument.

    String s = String.valueOf(100);   //100  数字转字符串
    WE ARE ALL IN THE GUTTER, BUT SOME OF US ARE LOOKING AT THE STARS
  • 相关阅读:
    VC++用Recordset MSPersist载入C#DataSet Save出来的xml失败,但载入VC Recordset Save出来的xml则没问题,怎么xml不通用呢?
    观察力、分析问题的能力、与人沟通的能力和资源整合能力
    [导入]有感于神童之神源
    军训系列二:两类人创业不容易成功
    运行微软的SOAP3.0的VC样例Samples30_ADOTM_Client报错,m_pSoapClient>Invoke时直接失败
    About IM software
    [导入][转]好企业是什么样?
    动网论坛v7.0.0SQL版竟然帯病毒!
    CZoneSoft出品: 音频视频在线录制系列之 AV留言本 简介
    递归算法在生成树型结构中,几乎完全属于无稽的算法
  • 原文地址:https://www.cnblogs.com/YKang/p/7273329.html
Copyright © 2011-2022 走看看