JAVA——Character&String&StringBuffer类

Strings are very commonly used in programming. There are many methods in Java, you can view API files

Character class

The Java language provides a packaging class Character class for the built-in data type char.

char ch = 'a';
// Unicode 字符表示形式
char uniChar = '\u039A'; 
// 字符数组
char[] charArray ={
    
     'a', 'b', 'c', 'd', 'e' };
//装箱:将一个char类型的参数传递给需要一个Character类型参数的方法 
//原始字符 'a' 装箱到 Character 对象 ch 中
Character ch = 'a';
 
// 原始字符 'x' 用 test 方法装箱
// 返回拆箱的值到 'c'
char c = test('x');

String class (the essence of a string is a character array)

  1. Create string
        //1、直接赋值
        String s="Hello";
        //2、使用构造方法赋值 String(String original)
        String s2=new String("hello");
        //3、String(char[] value)
        char [] chs={
    
    'h','e','l','l','o'};
        String s3=new String(chs);
        //4、String(char[] value, int offset, int count)
        //offset 从第一个下标开始取
        //count 取多少个字符
        String s4=new String(chs,1,1);

The difference between direct assignment and new
Direct assignment is to put the "string" directly into the constant pool. So as long as the strings are the same, the pointing is the same.
New is to open up space in the heap, and new corresponds to an address, and then points to the constant pool, so even if the assigned string is the same, the address is different.

  1. Connection string
//1、string1.concat(string2);
//2、"Hello," + " runoob" + "!"
  1. .length() method returns the length of the string
  2. Iterate over the string
   String s="welcome to java world HHFGH";
   char [] chs=s.toCharArray();
   //第一种遍历字符串的方式
   for(int i=0;i<chs.length;i++){
    
    
       System.out.print(chs[i]);
   }
   //直接遍历字符串
   for(int i=0;i<s.length();i++){
    
    
        System.out.print(s.charAt(i));
   }
  1. The difference between == and equal
    == For string, it is to compare the address between the two,
    equal is to really compare whether the two strings are equal
    6.`Some common methods
        String s="hello world java haha java";
        //获取字符串中的每个字符
        char c=s.charAt(6);
        //找大字符串中小字符串的第一次出现索引,如果没有,返回-1
        int index=s.indexOf("java");
        //indexOf(String str, int fromIndex)
        index=s.indexOf("java",index+1);
        //字符串截取
        String ss=s.substring(3);
        //截取指定的区间,[3,10)
        ss=s.substring(3,10);
        //比较时忽略大小写
        String s1="HELLO";
        System.out.println(s.equalsIgnoreCase(s1));
        //是否以...开头,一般用在判断姓名是否姓张....
        System.out.println(s.startsWith("l"));
        //是否以...结尾,一般用在判断文件是那种类型,.doc,.txt,.java
        System.out.println(s.endsWith("llo"));
        //判断字符串是否为空
        System.out.println(s.isEmpty());
        //trim去除字符串首尾空格
        System.out.println(s.trim().length());
        //字符串分割
        String names="jack,rose,tom,lucy,jhone";
        String [] strs=names.split(",");
        for(int i=0;i<strs.length;i++){
    
    
            System.out.println(strs[i]);
        }
        //字符串替换
        String ss="hello java i like java i enjoy java";
        System.out.println(ss.replace("java","c++"));
        System.out.println(ss.replaceAll("java","php"));
    }

[ Note ] Empty string is not the same as space

StringBuffer & StringBuilder

String & StringBuffer & StringBuilder 区别

-String When connecting, connect the strings in the two addresses in the string constant pool, assign them to the new address, and then point back to the variable. The original string space will not disappear.

  • When using the StringBuffer class, every time the StringBuffer object itself is operated, instead of generating a new object. That is, when connecting the string, do not open up space, directly add it to the original domain.
  • The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously). StringBuilder has a speed advantage compared to StringBuffer, it is recommended to use
  1. The string is static and is not frequently modified. String is preferred . Such as the declaration of constants, a small number of string operations (splicing, deleting, etc.).
  2. In -line the process and, if a large amount of operation of the string should be used StringBuilder operated string. You can't use String "+" to splice but use it to avoid generating a large number of useless intermediate objects, which consumes space and is inefficient in execution (new objects and recycling objects take a lot of time). Such as JSON packaging.
  3. In multi-line the process and, if a large amount of operation of the string, should use the StringBuffer . Such as HTTP parameter analysis and packaging, etc.
public static void stringBuilderConcat(){
    
    
        StringBuilder sb=new StringBuilder();
        long start=System.currentTimeMillis();
        //10000次所消耗的时间2毫秒
        //6毫秒
        for(int i=1;i<=100000;i++){
    
    
            sb.append("hello");
        }
        long end=System.currentTimeMillis();
        //消耗时间
        long time=end-start;
        System.out.println("StringBuilder字符串拼接所消耗的时间为:"+time+"毫秒");
    }

    public static void stringConcat(){
    
    
        String str="";
        long start=System.currentTimeMillis();
        //10000次所消耗的时间371毫秒
        //23840毫秒
        for(int i=1;i<=100000;i++){
    
    
            str+="hello";
        }
        long end=System.currentTimeMillis();
        //消耗时间
        long time=end-start;
        System.out.println("String字符串拼接所消耗的时间为:"+time+"毫秒");
    }

Guess you like

Origin blog.csdn.net/Christina_cyw/article/details/113004626