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

What are the common methods of Java-String

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

Share

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

This article mainly introduces "what are the common methods of Java-String". In daily operation, I believe many people have doubts about the common methods of Java-String. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the common methods of Java-String?" Next, please follow the editor to study!

I. string class

The String class is in the java.lang package, and java uses the String class to create a string variable that belongs to the object. Java does not inherit the final class declared by the String class. The String class object cannot be modified after creation and consists of 0 or more characters, enclosed between a pair of double quotes.

Second, the construction method of String class

1. Public String ()

The no-parameter constructor, which is used to create a String object of an empty string.

String str1 = new String ()

String str2 = new String ("asdf")

2. Public String (String value)

String str2 = new String ("asdf")

3. Public String (char [] value)

Char [] value = {'axiaqiaojia, bangzhu, cunning, cunning, d'}

String str4 = new String (value)

4. Public String (char chars [], int startIndex, int numChars)

Char [] value = {'axiaqiaojia, bangzhu, cunning, cunning, d'}

String str5 = new String (value, 1,2)

5. Public String (byte [] values)

Byte [] strb = new byte [] {65pm 66}

String str6 = new String (strb)

Third, common methods of string class

1. Public char charAt (int index)

Parameters.

Index-the index of the character.

Return value

Returns the character at the specified index.

Example

Public class Test {

Public static void main (String args []) {

String s = "www"

Char result = s.charAt (1)

System.out.println (result)

}

}

The execution results of the above procedures are as follows:

W

2. Public boolean equals (Object anObject)

Parameters.

AnObject-an object that is compared to a string.

Return value

Returns true; if the given object is equal to a string, otherwise returns false.

Example

Public class Test {

Public static void main (String args []) {

String Str1 = new String ("run")

String Str2 = Str1

String Str3 = new String ("run")

Boolean retVal

RetVal = Str1.equals (Str2)

System.out.println ("return value =" + retVal)

RetVal = Str1.equals (Str3)

System.out.println ("return value =" + retVal)

}

}

The execution results of the above procedures are as follows:

Return value = true

Return value = true

3. Public boolean endsWith (String suffix)

The endsWith () method tests whether the string ends with the specified suffix.

Parameters.

Suffix-the specified suffix.

Return value

Returns true; if the character sequence represented by the parameter is a suffix of the character sequence represented by this object, otherwise returns false. Note that if the parameter is an empty string or is equal to this String object (determined by the equals (Object) method), the result is true.

Example

Public class Test {

Public static void main (String args []) {

String Str = new String ("runooo")

Boolean retVal

RetVal = Str.endsWith ("run")

System.out.println ("return value =" + retVal)

RetVal = Str.endsWith ("ooo")

System.out.println ("return value =" + retVal)

}

}

The execution results of the above procedures are as follows:

Return value = false

Return value = true

4. Public boolean equalsIgnoreCase (String anotherString)

The equalsIgnoreCase () method is used to compare a string to a specified object, regardless of case.

Parameters.

AnObject-an object that is compared to a string.

Return value

Returns true; if the given object is equal to a string, otherwise returns false.

Public class Test {

Public static void main (String args []) {

String Str1 = new String ("run")

String Str2 = Str1

String Str3 = new String ("run")

String Str4 = new String ("RUN")

Boolean retVal

RetVal = Str1.equals (Str2)

System.out.println ("return value =" + retVal)

RetVal = Str3.equals (Str4)

System.out.println ("return value =" + retVal)

RetVal = Str1.equalsIgnoreCase (Str4)

System.out.println ("return value =" + retVal)

}

}

The execution results of the above procedures are as follows:

Return value = true

Return value = false

Return value = true

5. Public String replace (char oldChar,char newChar)

The replace () method replaces all oldChar characters that appear in the string with newChar characters and returns the new string after replacement.

Parameters.

OldChar-the original character.

NewChar-- new character.

Return value

The new string generated after replacement.

Public class Test {

Public static void main (String args []) {

String Str = new String ("hello")

System.out.print ("return value:")

System.out.println (Str.replace ('ostensible,' T'))

System.out.print ("return value:")

System.out.println (Str.replace ('lumped,' D'))

}

}

The execution results of the above procedures are as follows:

Return value: hellT

Return value: heDDo

6. Public String toLowerCase ()

The toLowerCase () method converts the string to lowercase.

Parameters.

None

Return value

Converts to a lowercase string.

Public class Test {

Public static void main (String args []) {

String Str = new String ("WWW")

System.out.print ("return value:")

System.out.println (Str.toLowerCase ())

}

}

The execution results of the above procedures are as follows:

Return value: www

At this point, the study of "what are the common methods of Java-String" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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