Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common ways to use strings in java

Shulou Source: shulou.com Published: 2022-06-02 13:09:03 10月29日 Update

This article mainly explains the "what are the common java string use methods", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java common string use what are" it!

First, use the length () method to get the length of the string

Public class maintest {public static void main (String [] args) {String str = "abcdefg"; / / length (): count the number of characters in the current string int I = str.length (); System.out.println (I);}}

Second, use the indexOf () method to find the position of the specified character in the string

Public class maintest {public static void main (String [] args) {String str = "abcdefg"; / / indexOf (): find the position of the specified character in the string int index = str.indexOf ("a"); System.out.println (index);}}

Third, use the toUpperCase () method to capitalize the characters in the string

Public class maintest {public static void main (String [] args) {String str = "abcdefg"; / / lowercase to uppercase / / toUpperCase (): changes characters in a string to uppercase String str = str.toUpperCase (); System.out.println (str);}}

Fourth, use the toLowerCase () method to make the characters in the string lowercase

Public class maintest {public static void main (String [] args) {/ / toLowerCase (): change characters in a string to lowercase String str = "WWMMDDHH"; String str1 = str3.toLowerCase (); System.out.println (str);}}

Use the substring () method to intercept the string

Public class maintest {public static void main (String [] args) {String str = "abcdefg"; / / substring: intercept the string String str = "abcdefg"; String str = str5.substring (0,3); System.out.println (str); String str = str5.substring (3); System.out.println (str);}}

Replace the specified content in the current string with the replaceAll () method

Public class maintest {public static void main (String [] args) {String str = "abcdefg"; String str = str5.replaceAll ("abc", "xyz"); System.out.println (str);}}

7. Use the trim () method to remove the spaces at both ends of the current string

Public class maintest {public static void main (String [] args) {String str = "abc def"; System.out.println (str.length ()); String str1 = str9.trim (); System.out.println (str); System.out.println (str1);}}

String + string equals concatenation

Public class maintest {public static void main (String [] args) {String str1 = "123"; String str2 =" 100"; System.out.println (str1+str2);}}

Change a string into an integer

Public class maintest {public static void main (String [] args) {String str1 = "123"; String str2 =" 100"; int num1 = Integer.parseInt (str1); int num2 = Integer.parseInt (str2); System.out.println (num1+num2);}}

Use charAt () to find the character at the position in the specified string

Public class maintest {public static void main (String [] args) {String str1000 = "abcde"; / / charAt: find the character char char = str1000.charAt (2); System.out.println (char);}} in the specified string

Comparison of java strings

Generally speaking, the comparison of strings in java is the practice of comparing references and equals comparing values. (equals compares addresses for other reference types, because the equals method of object compares references, but the comparison results are different for different declaration method strings.

For example:

String str1=new String ("a"); String str2=new String ("a"); str1==str2 output falsestr1.equals (str2) output true and if you declare String str1= "a"; String str2= "a"; str1==str2 output truestr1.equals (str2) output true

This is because the equals method is also compared to the string class that references the method when it is implemented, the first declaration method is equivalent to declaring two objects, using'='to compare the reference output is false because their values are the same when using equals to compare the values, output true.

The second case is not because the comparison is different from the first, but because the declaration is different, and the second declaration method has the phenomenon of heap or stack sharing when declaring. That is to say, if it is declared as a class attribute, he will check whether there is a string in the stack with the same structure as what is now declared. If so, point the address directly to the existing memory address. The principle of the local variable declared inside the method is the same except that it is stack sharing. Corresponding to the above is the example:

The first situation:

(1) declare an object of type string str1, which instantiates an object of type string in memory. (2) declare an object of type String str2, and this operation will instantiate another object of type String in memory, independent of the first object. (3) compare the references of two objects, because they are independent of each other, so output false (4) compare the values of two objects, although they are independent memory areas, but have a common memory structure (value), output true

The second situation:

(1) declaring a string str1 instantiates an object of type String. (2) declare another string, which now checks for variables with the same memory structure in the same scope, and if so, points the address directly to him. (3) = = compare that they have the same address, (4) equals compare that they have the same memory structure (value).

Thank you for your reading, these are the contents of "what are the common ways to use java strings?" after the study of this article, I believe you have a deeper understanding of the common use of java strings, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Character string method object output memory type same address time usage common location case example structure two content stack uppercase Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Microsoft Huawei NVidia