Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the JAVA API utility class String

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to use the JAVA API utility class String, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The concept of String class string

To put it simply, a string is a string composed of multiple characters (char). We can naturally use the char [] array to save a string. But when manipulating a string, such as inserting a character into a string, you need to move back and so on. To deal with this problem, C++ introduces the string class, and similar Java also has the String class. The string class is in java.lang, so there is no need for the import import package.

The special features of Java String

Suppose we define four strings so that their contents are the same. The only difference is whether or not to use the new statement. One thing to note here is that in order to improve the running speed, Java uses "object pool" to store string constants, that is, to open up a special string pool to store strings. When creating a string, the system will look in the string pool for the existence of strings with the same content, if there is a direct use of the object. That is to say, str1 and str2 point to the same string. However, str3 and str4 do not point to a string through new. More noteworthy is the immutability of String content. That is, each time you change the content of a string, it produces a new object, or points to a string object that already exists in the string pool.

String str1= "a string"; String str2= "a string"; String str3=new String ("a string"); String str4=new String ("a string"); str1= "another string"; / / the original object "a string" still exists, which causes memory consumption the basic method string of the String class, string creation 1. String () / generates an empty string String a=new String () 2. String (String original) / / copy the original string directly String a = "java", String b = new String (a); 3.String (char [], start,count) / / the last two parameters of the string array assignment can be omitted as the starting position and length char ch [] = {'axiajiaoyuzhuo]]; String s1=new String (ch); / / s1roomabvcdeatorString s2=new String (ch,2,3) / / s2=vcd The common method / / gets the length String s = "sabsda"; int cnt=s.length (); boolean equals (Azov b); / / compares whether the contents of two strings are the same, case-sensitive boolean equalsIgoreeCase () / / ignores case comparison / * the difference between equal and = = is that equal only judges whether the content is the same, but = = whether the comparison points to the same object * / String str1= "a string"; String str2= "a string"; String str3=new String ("a string") String str4=new String ("a string"); equals (str1,str2) = = 1 (str1==str2) = = 1equals (str3,str4) = = 1 (str3==str4) = = 0 leading int compareTO (int compareTOUgnoreCase b); / / lexicographical comparison if ab returns a positive number int compareTOUgnoreCase (); / / case-insensitive boolean startsWith (string a); / / whether boolean endsWiith starts with a (String a) / / whether to find 1.int indexOf (char ch) 2.int indexOf (char ch,int position) with an ending / / string; / / string modification / / it should be noted that the string content is immutable, if the hard change will produce a new string but the original string will not change. So all operations that change the string will return a new string object instead of modifying 1. 0 on the original string. Connecting String str= "This" .concat ("a") concat is similar to +; 2 instead of str.replace ('axiomanageme') replace an in str with b3 to remove the header and trailing spaces str.trim (); 4 str.toLowerCase () / lowercase 5 str.toUUpperCase () / / the above is how the JAVA API utility class String is used, have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report