[Java] Methods frequently used in the Java String class

foreword

The String class is in the java.lang package. Java uses the String class to create a string variable, and the string variable belongs to an object. String class object cannot be modified after creation, StringBuffer & StringBuilder class. At this time, we will ask, why doesn't our String variable assignment just change? In fact, it is not. After assignment, a new object will be generated to store new content. The original object is still in memory, but it is no longer pointing to it. Then this object will become garbage memory, which will be recycled by the Java virtual machine at a certain moment. Enclosed between a pair of double quotes.

Creation of String string variables:

Declaration: String variable name;

String str;

Declare and initialize:
String variable name = "initial value";

String str = "我的_Java例子";

1、int length();

Syntax: string variable name.length();
return value is int type. Get the number of characters in a string (Chinese, English, spaces, and escape characters are all characters, counted in the length)

The code is as follows (example):

String a="我的_Java例子";
        int l = a.length();
        System.out.println(l);
输出的结果为9

2. char charAt(value);

Syntax: string name.charAt(value); return value is char type. Extract the character at the specified position from the string

The code is as follows (example):

String str="我的_Java";
        char c = str.charAt(3);
        System.out.println("指定字符为:" + c);
运行结果:指定字符为:J

3、char toCharArray();

Syntax: string name.toCharArray(); return value is char array type. turns a string into an array of chars

The code is as follows (example):

String str="我的了"char c[] = str.toCharArray(); 
for (int i = 0; i < c.length; i++)
System.out.println("转为数组输出:" + c[i]);
运行结果:
转为数组输出:我
转为数组输出:忘
转为数组输出:

4. int indexOf("character")

Syntax: string name.indexOf("character"); string name.indexOf("character", value); find out whether a specified string exists, and return the position of the string, if not, return -1.
  in lastIndexOf("character") Get the subscript of the last occurrence of the specified content.
The code is as follows (example):

String str = "I am a good student";
        int a = str.indexOf('a');//a = 2
        int b = str.indexOf("good");//b = 7
        int c = str.indexOf("w", 2);//c = -1
        int d = str.lastIndexOf("a");//d = 5
        int e = str.lastIndexOf("a",3);//e = 2
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
运行结果:
转为数组输出:2
转为数组输出:7
转为数组输出:-1
转为数组输出:5
转为数组输出:2

5. toUpperCase(); toLowerCase(); string case conversion

String str="hello world";
System.out.println("将字符串转大写为:" + str.toUpperCase());
System.out.println("将字符串转换成小写为:" + str.toUpperCase().toLowerCase());

Operation result:

将字符串转大写为:HELLO WORLD
将字符串转换成小写为:hello world

6. String[] split("character")

Splits this string based on matches of the given regular expression. Form a new String array.

String str = "boo:and:foo";
String[] arr1 = str.split(":");
String[] arr2 = str.split("o");
运行结果:
  arr1  //{ "boo", "and", "foo" }
  arr2  //{ "b", "", ":and:f" }

7、boolean equals(Object anObject)

Syntax: String variable name.equals (string variable name); The return value is Boolean. So here is an if demonstration. Compares two strings for equality, returns a boolean

String str = "hello";
        String str1 = "world";
        if (str.equals(str1)) {
    
    
            System.out.println("这俩字符串值相等");
        } else {
    
    
            System.out.println("这俩字符串值不相等");
        }
运行结果:
这俩字符串值不相等

8、trim()

Remove the left and right spaces of the string replace(char oldChar, char newChar); the new character replaces the old character, which can also achieve the effect of removing spaces.

String str = "      我的_Java例子         ";  
System.out.println("去掉左右空格后:" + str.trim());

operation result:

去掉左右空格后:我的_Java例子

The second type:

String str = "       我的_Java例子         ";  
System.out.println("去掉左右空格后:" + str.replace(" ","")); 

operation result:

去掉左右空格后:我的_Java例子

9. String substring(int beginIndex,int endIndex) intercepts the string

String str = "123我的_Java例子456";
        System.out.println("截取后的字符为:" + str.substring(0, 3));// 截取0-3个位置的内容   不含3
        System.out.println("截取后字符为:" + str.substring(2));// 从第3个位置开始截取    含2

operation result:

截取后的字符为:123
截取后字符为:3我的_Java例子456

10、boolean equalsIgnoreCase(String)

Ignoring case, compare whether the values ​​of two strings are exactly the same, and return a boolean value

String str = "123我的_Java例子456";
        System.out.println("截取后的字符为:" + str.substring(0, 3));// 截取0-3个位置的内容   不含3
        System.out.println("截取后字符为:" + str.substring(2));// 从第3个位置开始截取    含2

operation result:

截取后的字符为:123
截取后字符为:3我的_Java例子456

11、boolean contains(String)

Determines whether a string contains the specified content and returns a Boolean value

   String str = "HELLO WORLd";
    String str1 = "WO";
    if (str.contains(str1)) {
    
    
        System.out.println("str内容中存在WO");
    } else {
    
    
        System.out.println("抱歉没找着");
    }

operation result:

str内容中存在WO

12、boolean startsWith(String)

Tests whether this string starts with the specified prefix. returns a boolean value

   String str = "HELLO WORLd";
    String str1 = "HE";
    if (str.startsWith(str1)) {
    
    
        System.out.println("str内容中存在HE前缀开头");
    } else {
    
    
        System.out.println("抱歉没找着");
    }

operation result:

str内容中存在HE前缀开头

13、boolean endsWith(String)

Tests whether this string ends with the specified suffix. returns a boolean value

String str = "我的例子";
        String str1 = "例子";
        if (str.endsWith(str1)) {
    
    
            System.out.println("str内容中存在\'例子\'后缀结束");
        } else {
    
    
            System.out.println("抱歉没找着");
        }

operation result:

str内容中存在'例子'后缀结束

14. The replace method is mentioned above, and then continue to add

String replaceAll(String,String) Replace all the content with the specified content
String replaceFirst(String,String) Replace the first occurrence of a certain content with the specified content

String str = ",,,,,,我的例子,,,,, ";
        System.out.println("改变后:" + str.replaceAll(",", "a"));

operation result:

改变后:aaaaaa我的例子aaaaa 
  String str = ",,,,,,我的例子6不,,,,, ";
        System.out.println("改变后:" + str.replaceFirst("6不", "很6"));

operation result:

  改变后:,,,,,,我的例子很6,,,,, 

Guess you like

Origin blog.csdn.net/u011397981/article/details/131850588