In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the String class in java", the content is simple and clear, hoping to help you solve your doubts, let the editor lead you to study and learn "what is the String class in java" this article.
What is a string?
A string or string (String) is a string of characters consisting of numbers, letters, and underscores. It is generally marked as s = "a1a2 an" (n > = 0). It is the data type that represents text in a programming language. In programming, a string is a continuous sequence of symbols or values, such as a symbol string (a string of characters) or a binary number string (a string of binary digits). It can extract a character in a string by certain methods in java language.
The Common assignment method of string Direct assignment method
String variable name = "initial value"
This method of assignment is often used by us.
Public static void main (String [] args) {String str1= "hello world"; System.out.println (str1);}
Constructor to create
Format:
String variable name = new String (initial value)
Public static void main (String [] args) {String str2=new String ("SWPU YYDS"); System.out.println (str2);}
Note: initial values are not limited to constant strings, but can also be arrays.
For example, a character array:
Public static void main (String [] args) {char ch [] = {'Seven (ch); System.out.println (str3);}
Another example is a byte array:
Public static void main (String [] args) {byte a [] = {94 String str3=new String (a); System.out.println (str3);}
In addition, some elements of a character array or byte array can also be converted into strings by this construction.
Public static void main (String [] args) {char ch [] = {'a 'axiomagem []); String str4=new String (ch,1,3); / / 1 is the start position of the string, 3 is the length of the string System.out.println (str4);}
Comparison of strings is equal
Using "=" to determine whether the values assigned to two int variables are equal in the int variable can be determined.
Public static void main (String [] args) {int aplomb 520; int baud 520; System.out.println ("axiomb:" + (aquifolb));}
Then some people will think that the comparison of strings can also be compared with "=".
As shown in the figure:
Public static void main (String [] args) {String str5= "abcdef"; String str6= "abcdef"; System.out.println ("whether str5 and str6 are equal" + (str5==str6));}
So does the string rely on "=" to judge? The result is obviously yes.
Like this code:
Public static void main (String [] args) {String str8= "abcdef"; String str7=new String ("abcdef"); System.out.println ("str7 and str8 are equal" + (str7==str8));}
Public static void main (String [] args) {String str8=new String ("abcdef"); String str7=new String ("abcdef"); System.out.println ("whether str7 and str8 are equal" + (str7==str8);}
Why does this happen when you use "="?
In fact, String uses = = comparison not to compare string contents, but to compare whether two references point to the same object.
For example, there are two baskets called A _ Magi B; there are five apples in each basket, and the two baskets are regarded as two different classes An and B, and the comparison is whether the class is the same, not whether the content in the class is the same.
To compare whether the string contents are equal, we have to use the equals method provided by the String class
Public static void main (String [] args) {String str8= "abcdef"; String str7=new String ("abcdef"); System.out.println ("str7 and str8 are equal" + (str7.equals (str8);}
Public static void main (String [] args) {String str8=new String ("abcdef"); String str7=new String ("abcdef"); System.out.println ("str7 and str8 are equal" + (str7.equals (str8);}
String constant pool
In the string assignment operation mentioned above
Direct assignment Why use "=" for comparison: some are true and some are false?
In fact, when we use the direct assignment mode to instantiate the object of the String class, then the instantiated object (string content) will be automatically saved to the string constant pool, and if we continue to declare the String class object using the direct assignment mode next time, if there is a specified content in the object pool, it will be referenced directly if not. Open up a new string object and save it in the object pool for next use.
Public static void main5 (String [] args) {String str5= "abcdef"; String str6= "abcdef"; System.out.println ("whether str5 and str6 are equal" + (str5==str6));}
When we use the construction method, first, when instantiating the object of the String class, we first look for the string content in the character constant pool, if so, we do not need to put it in the string constant pool, and then open up a memory space on the heap, which points to the content on the string constant pool, while the object on the stack points to the newly opened memory space.
Public static void main6 (String [] args) {String str8= "abcdef"; String str7=new String ("abcdef"); System.out.println ("str7 and str8 are equal" + (str7==str8));}
Note: when using direct assignment, only one piece of heap memory space is opened up, and the string object can be automatically saved in the object pool for next use. When using the construction method, it opens up two heap memory spaces, which are not automatically saved in the object pool, but can be manually pooled using the intern () method.
An example of a string constant pool
To better understand the string constant pool, here are a few examples to practice.
Example one
Public static void main (String [] args) {String str1= "abcdef"; String str2= "abc" + "def"; System.out.println (str1==str2);}
two。 Example two
Public static void main (String [] args) {String str1= "abcdef"; String str2= "abc"; String str3=str2+ "def"; System.out.println (str1==str3);}
Note that str2 is a variable. You can't know what the value is when you compile it. You won't know it until you run it.
3. Example three
Public static void main (String [] args) {String str1= "abcdef"; String str2= "abc" + new String ("def"); System.out.println (str1==str2);}
4. Example 4
Public static void main (String [] args) {String str1= "abcdef"; String str2=new String ("abc") + new String ("def"); System.out.println (str1==str2);}
5. Example five
Public static void main (String [] args) {String str1= "abc"; String str2=str1; str1= "def"; System.out.println (str1==str2);}
Immutability of a string
A string is an immutable object. Its content cannot be changed.
The internal implementation of the String class is also based on char [], but the String class does not provide set methods to modify the internal character array.
Such as the code:
Public static void main (String [] args) {String str = "hello"; str = str + "world"; str + = "!!"; System.out.println (str);}
The result seems simple, but a lot of operations have been done at the bottom.
Step one:
Step 2:
Step 3:
So what if we need to modify the string, for example, the existing string str = "Hello" and want to change it to str = "hello"?
The easiest way is to create a new string with the help of the original string
Public static void main (String [] args) {String str = "Hello"; str = "h" + str.substring (1); System.out.println (str);}
Why is String immutable?
It is convenient to implement string object pool. If the String is variable, then the object pool needs to consider when to deep copy the string.
Immutable objects are thread-safe.
Immutable objects are more convenient to cache hash code, and can be saved to HashMap more efficiently as key.
Comparison of common operation strings of strings
Equals method
This method compares the contents of strings and distinguishes the size of characters.
Public static void main (String [] args) {String str= "Hello"; String str1= "hello"; System.out.println (str.equals (str1));}
EqualsIgnoreCase method
This method compares the contents of strings but does not distinguish between the size of characters.
Public static void main (String [] args) {String str= "Hello"; String str1= "hello"; System.out.println (str.equalsIgnoreCase (str1));}
CompareTo method
The compareTo () method is a very important method in the String class, which returns an integer, which returns three types of content according to the size relationship: 0 if equal, less than 0 if the string is less than the comparison, and a value greater than 0.
Public static void main (String [] args) {String str1= "Hello"; String str2= "hello"; System.out.println (str1.compareTo (str2));}
Public static void main (String [] args) {String str2= "Hello"; String str1= "hello"; System.out.println (str1.compareTo (str2));}
Public static void main (String [] args) {String str2= "hello"; String str1= "hello"; System.out.println (str1.compareTo (str2));}
Search for strings
Contains () method
This method can determine whether the string has a substring. If it returns true;, it returns false.
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "xass"; System.out.println (str1.contains (str2));}
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "acbaskj"; System.out.println (str1.contains (str2));}
IndexOf () method
This method causes the string to find the location of the string from scratch, returns the starting index of the location when found, and returns-1 if it is not checked.
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askj"; System.out.println (str1.indexOf (str2));}
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askjsass"; System.out.println (str1.indexOf (str2));}
LastIndexOf (String str) method
Look for substrings from back to front
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askj"; System.out.println (str1.lastIndexOf (str2));}
LastIndexOf (String str,int fromIndex) method
Look from the specified location from back to forward.
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askj"; System.out.println (str1.lastIndexOf (str2,1));}
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askj"; System.out.println (str1.lastIndexOf (str2,13));}
StartsWith (String prefix)
Determine whether to start with the specified string. If yes, return true. If not, return false.
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "askj"; System.out.println (str1.startsWith (str2));}
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "ac"; System.out.println (str1.startsWith (str2));}
StartsWith (String prefix,int toffset)
Judge whether to start with the specified string from the specified position
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "bask"; System.out.println (str1.startsWith (str2,2));}
EndsWith (String suffix)
Determine whether to end with a specified string
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "bask"; System.out.println (str1.endsWith (str2));}
Public static void main (String [] args) {String str1= "acbaskjcbaskd"; String str2= "skd"; System.out.println (str1.endsWith (str2));}
String substitution
ReplaceAll (String regx,String replacement)
Replace all specified contents in the string
Public static void main (String [] args) {String str = "helloworld"; System.out.println (str.replaceAll ("l", "_"));}
ReplaceFirst (String regx,String replacement)
Replace the first specified content in the string
Public static void main (String [] args) {String str = "helloworld"; System.out.println (str.replaceFirst ("l", "_"));}
Note: since the string is an immutable object, substitution does not modify the current string, but produces a new string.
String splitting
Split (String regex)
Split all strings
Public static void main (String [] args) {String str = "hello world hello swpu"; String [] result = str.split (""); for (String s: result) {System.out.println (s);}}
Note: some special characters as delimiters may not be segmented correctly and need to be escaped
Such as:
Public static void main (String [] args) {String str = "192.168.1.1"; String [] result = str.split ("\\."); for (String s: result) {System.out.println (s);}}
Note:
The characters "|", "*" and "+" all have to be escaped, preceded by "\". And if it's "." then it has to be written as "\". If there are multiple delimiters in a string, you can use "|" as the hyphen split (String regex,int limit)
Split part of the string. The length of the array is the limit limit.
Public static void main (String [] args) {String str = "hello world hello swpu"; String [] result = str.split ("", 3); for (String s: result) {System.out.println (s);}}
String interception
Substring (int bedinIndex)
From the specified index to the end
Public static void main (String [] args) {String str= "helloworld"; System.out.println (str.substring (3));}
Substring (int beginIndex,int endIndex)
Intercept part of a string
Public static void main (String [] args) {String str= "helloworld"; System.out.println (str.substring (3pm 5));}
Note: this interval is left closed and right open: [3pc5) does not contain 5 subscript
The above is all the content of the article "what is the String class in java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.