In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use the String class in Java", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use the String class in Java" this article.
Most programming languages can handle strings (String). A string is an ordered collection of characters, such as "Hello World!". In Java, strings are stored as String class objects. You can implement string-related operations by calling the methods of the string object.
The String class is included in the java.lang package. This package automatically import when Java starts, so it can be treated as a built-in class (built-in class). We do not need to explicitly use import to introduce the String class.
Create a string
We used classes to create objects before. When you need to note, you don't need the new keyword to create String class objects. For example:
Public class Test {public static void main (String [] args) {String s = "Hello World!"; System.out.println (s);}}
In fact, when you write a "Hello World" expression, the object is already created in memory. If you use new String ("Hello World!"), a string object is created repeatedly.
An Object
The String class is the only class that does not need the new keyword to create objects. You need to be careful when using it.
String operation
You can use + to concatenate strings, such as:
"abc" + s
String operations are mostly implemented by the corresponding methods of the string, such as the following methods:
Method effect
S.length () returns the length of the s string
S.charAt (2) returns the character in the s string with the subscript 2
S.substring (0,4) returns the substring of subscript 0 to 4 in the s string
S.indexOf ("Hello") returns the subscript of the substring "Hello"
S.startsWith ("") determines whether s starts with a space.
S.endsWith ("oo") determines whether s ends with "oo"
S.equals ("Good World!") Judge whether s equals "Good World!"
= = can only determine whether the string is saved in the same location. You need to use equals () to determine whether the contents of the string are the same.
S.compareTo ("Hello Nerd!") Compare the s string with "Hello Nerd!" The order in the dictionary
Returns an integer. If 0, s is in "Hello Nerd!" After that
If = = 0, it means that s and "Hello Nerd!" Equal.
S.trim () removes the space string before and after s and returns a new string
S.toUpperCase () converts s to uppercase letters and returns a new string
S.toLowerCase () converts s to lowercase and returns a new string
S.replace ("World", "Universe") replaces "World" with "Universe" and returns a new string
Immutable object
String class objects are immutable objects (immutable object). Programmers cannot modify existing immutable objects. We can also create immutable objects ourselves, as long as there is no way to modify the data in the interface.
However, string class objects do have the ability to edit strings, such as replace (). These editing functions are achieved by creating a new object, rather than by modifying the original object. For example:
S = s.replace ("World", "Universe")
The call to s.replace () on the right creates a new string "Hello Universe!" and returns the object (reference to). By assigning, the reference s will point to the new string. If there are no other references to the original string "Hello World!", the original string object will be garbage collected.
Immutable object
Java API
Java provides many powerful packages. An important aspect of Java learning is to understand these packages and the API (Application Programming Interface) they contain. The String class is defined in java.lang.String.
The above is all the content of the article "how to use String classes 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.