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 understand the String class in JavaAPI

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to understand the String class in JavaAPI, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

String class of JavaAPI

[size=10.5000pt] 1. Belong to the package java.lang.String, there are no subclasses. Features: once initialized, it cannot be changed.

There are two ways to create class objects:

String S1 = "abc"

String S1 = new String ("abc")

How memory exists:

The statement String Str = "jack" first checks whether the string object "jack1" is stored in the string constant pool. If it does not exist, the string object is created in the string constant pool, and if there is a memory address value that returns the string directly.

String str3 = new String ("jack") this statement creates two objects, first checking whether the string object jack exists in the string constant pool, creating it if not, and returning the memory address value if it does. Once created, the new String statement opens up a string object in the heap memory. There are two objects in total.

Note: equals compares the memory address of the object, but the String class overrides the equals method in the Object class to determine whether the string is the same. S1.equals (S2) is true.

[size=10.5000pt] II. Methods of manipulating strings

[size=10.5000pt] 1. Acquisition method:

The length of the 1.1:int length () string

Characters in a specific position of 1.2:char charAt (int index) (corner mark out of bounds)

1.3:int indexOf (int ch) position of a specific character (overload) (more than one character returns the first occurrence) No return-1 was found

Int indexOf (int ch,int fromIndex) starts searching from a specified location

Position of the int indexOf (String str) subcharacter (overload)

Int indexOf (iString str,int fromIndex) starts searching from a specified location

The position of the last character of 1.4:int lastIndexOf (int ch)

[size=10.5000pt] 2. Judgment method:

2.1: whether the string contains a substring

The boolean contains (CharSequences) CharSequences character sequence can also be int indexOf (String str), and a return of-1 indicates that it is not included.

2.2 whether to start with the specified content Boolean startsWith (String str)

2.3 whether to end with the specified content boolean endsWith (String str)

Whether boolean isEmpty () is empty or has a length of 0 such as "" null V1.6 "

2.5 whether boolean equals (Object anObject) is equal

Boolean equalsIgnoreCase (String anotherString) ignores case equality

[size=10.5000pt] III. Conversion function

3.1: convert an array of characters to a string

1) Constructor: String (char [])

String (char [], offset,count) converts part of the character array into a string (starting with offset, number of count)

Example:

Char [] arr = {'axie'}

String str = new String (arr,1,3)

[size=10.5000pt] 2) static method:

[size=10.5000pt] 3) Static String copyValue Of (char [])

[size=10.5000pt] 4) Static String copyValue Of (char [], offset,count)

Static String Value Of (char [])

3.2: convert a string to a character array

Char [] toCharArray ()

3.3 convert byte array to string

String (byte [])

String (byte [], offset,count)

3.4 string to byte array

Byte [] getBytes ()

3.5: convert basic data types to strings

Static String Value Of (int)

Static String Value Of (double)

2 + ""

IV. Replacement method

String replace (char old,char,new)

String s = "hello java"; String S1 = s.replace (eMagol k); s is hello java, S1 is hkllo java

String S1 = s.replace ("java", "word")

[size=10.5000pt] 5. Cutting method

String [] split (regex)

[size=10.5000pt] sixth, substring, get

String substring (int beginindex,int endindex) contains the head but not the tail

String substring (int begin)

[size=10.5000pt] VII. Conversion, removal of spaces, comparison

7.1: case conversion

String toUpperCase ()

String toLowerCase ()

7.2: remove multiple spaces at both ends

String trim ()

7.3 compare the natural order of two strings

Int CompareTo () equals 0 less than a negative number greater than-positive comparison ASCII

After reading the above, have you mastered how to understand the String class in JavaAPI? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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