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
  • 相关阅读:
    blender 2.8 [学习笔记-033] 编辑模式-滑动边线
    blender 2.8 [学习笔记-032] 编辑模式-随机(光滑)
    blender 2.8 [学习笔记-031] 编辑模式-光滑
    blender 2.8 [学习笔记-030] 编辑模式-旋转
    blender 2.8 [学习笔记-029] 编辑模式-多边形建形
    TurtleBot3使用课程-第一节a(北京智能佳)
    ViperX 300 Robot Arm 机械臂 “5自由度和360°全方位旋转”
    TurtleBot 3 & 2i ROS开源实验平台
    在人工智能实验平台下对主流舵机的简介
    走进车联网,你将更深入地了解无人车和自动驾驶技术
  • 原文地址:https://www.cnblogs.com/YKang/p/7273329.html
Copyright © 2011-2022 走看看