JAVA-Numbers and Strings (5) Strings

A string is a combination of characters. In Java, a string is a class, so the strings we see are all objects.
Common methods for creating strings:

  1. Whenever a literal value appears, the virtual machine will create a string
  2. Call the String constructor to create a string object
  3. String splicing through the + plus sign will also create a new string object
package character;
 
public class TestString {
    
    
 
    public static void main(String[] args) {
    
    
        String garen ="盖伦"; //字面值,虚拟机碰到字面值就会创建一个字符串对象
         
        String teemo = new String("提莫"); //创建了两个字符串对象
         
        char[] cs = new char[]{
    
    '崔','斯','特'};
         
        String hero = new String(cs);//  通过字符数组创建一个字符串对象
         
        String hero3 = garen + teemo;//  通过+加号进行字符串拼接
    }
}

immutable

Immutable means immutable,
such as creating a string object
String garen = "Galen";
the specific meaning of immutable refers to:
cannot increase the length,
cannot reduce the length,
cannot insert characters,
cannot delete characters,
cannot modify characters,
once the string is created. , The content inside can never be changed

String behaves like a constant

String length

String a = "你好啊";
System.out.println(a.length())

The index of the string

Use: charAt() method to get a single character

public class TestNumber {
    
    
	public static void main(String args[]) {
    
    
		String item = "浙江温州最大皮革厂倒闭了";
		char a = item.charAt(0);
		System.out.println(a);
		//结果为“浙”
	}
}

Use: toCharArray() method to get character array

		String item = "浙江温州最大皮革厂倒闭了";
		char[] a = item.toCharArray();
		System.out.println(a);

Intercept substring

substring(strat,end)

		String item = "浙江温州最大皮革厂倒闭了";
		String a = item.substring(2);
		System.out.println(a);

Insert picture description here

Separate

	public static void main(String args[]) {
    
    
		String item = "浙江,温州,最大皮革厂倒闭了";
		String [] a = item.split(",");
		for(String i :a) {
    
    
			System.out.println(i);
		}
	}

Insert picture description here

Remove leading and trailing spaces

trim

package character;
    
public class TestString {
    
    
    
    public static void main(String[] args) {
    
    
   
        String sentence = "        盖伦,在进行了连续8次击杀后,获得了 超神 的称号      ";
         
        System.out.println(sentence);
        //去掉首尾空格
        System.out.println(sentence.trim());
    }
}

Case

toLowerCase becomes all lowercase
toUpperCase becomes all uppercase

package character;
    
public class TestString {
    
    
    
    public static void main(String[] args) {
    
    
   
        String sentence = "Garen";
         
        //全部变成小写
        System.out.println(sentence.toLowerCase());
        //全部变成大写
        System.out.println(sentence.toUpperCase());
         
    }
}

Positioning

indexOf judges whether the position where a character or a substring appears
contains contains a substring

package character;
     
public class TestString {
    
    
     
    public static void main(String[] args) {
    
    
    
        String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
  
        System.out.println(sentence.indexOf('8')); //字符第一次出现的位置
          
        System.out.println(sentence.indexOf("超神")); //字符串第一次出现的位置
          
        System.out.println(sentence.lastIndexOf("了")); //字符串最后出现的位置
          
        System.out.println(sentence.indexOf(',',5)); //从位置5开始,出现的第一次,的位置
          
        System.out.println(sentence.contains("击杀")); //是否包含字符串"击杀"
          
    }
}

replace

replaceAll replace all replaceFirst replace
only the first

package character;
    
public class TestString {
    
    
    
    public static void main(String[] args) {
    
    
   
        String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
 
        String temp = sentence.replaceAll("击杀", "被击杀"); //替换所有的
         
        temp = temp.replaceAll("超神", "超鬼");
         
        System.out.println(temp);
         
        temp = sentence.replaceFirst(",","");//只替换第一个
         
        System.out.println(temp);
         
    }
}

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108715100