Common Methods of the string class.

Source: Internet
Author: User
Tags array to string

1. Character arrays and strings.

A string can be converted into a character array. Similarly, a character array can be converted into a character string.

The following operations are provided in the string class.

1) convert the string into a character array: Public char [] tochararray ();

2) character array to string: Public String (char [] value)

Public String (char [] value, int offset, int count); // represents the Count characters starting from the offset position, and is combined into a string.

Public class stringapidemo01 {public static void main (string ARGs []) {string str1 = "hello"; // defines the string char C [] = str1.tochararray (); // convert a string to a character array for (INT I = 0; I <C. length; I ++) {// cyclically output system. out. print (C [I] + ",");} system. out. println (""); // wrap string str2 = new string (c); // convert all character arrays to string str3 = new string (C ); // convert some character arrays to string system. out. println (str2); // output string system. out. println (str3); // output string }}
Running result:
H, E, l, l, O
Hello
El

The preceding two methods are used: String-to-array and array-to-string.

 

2. Extract the characters at the specified position from the string.

If you want to perform this operation, the return type must be char.

Usage: Public char charat (INT index)

Public class stringapidemo02 {public static void main (string ARGs []) {string str1 = "hello"; // defines the string object system. out. println (str1.charat (3); // retrieves the fourth character from the string }};
Output result:
L

3. String and bety array conversion.

Byte array, which is often used in I/O

In the string class, the following methods are provided to convert strings and byte arrays.

1) string to byte array: Public byte getbytes ();

2) Change a byte array to a string:

Convert all arrays into strings: Public String (byte [] Byte );

Convert some arrays into strings: Public String (char [] Byte, int offset, int count );

Public class stringapidemo03 {public static void main (string ARGs []) {string str1 = "hello"; // defines the string byte B [] = str1.getbytes (); // convert the string to the byte array system. out. println (new string (B); // convert all byte arrays to the string system. out. println (new string (B, 1, 3); // convert part of the byte array to a string }};

4. Obtain the length of an array.

Public int length ();

Public class stringapidemo04 {public static void main (string ARGs []) {string str1 = "Hello lixinghua"; // defines the string variable system. out. println ("\" "+ str1 +" \ ":" + str1.length ());}};

5. Check whether the specified string exists.

In practice, it is often used to determine whether a string contains certain content. You can use the following method:

1) search from the beginning: Public int indexof (string Str); returns the subscript starting with the string.

2) search from the specified position: Public int indexof (string STR, int fromindex); // return an int data, indicating the specific position of the string. If not, -1,

Public class stringapidemo05 {public static void main (string ARGs []) {string str1 = "abcdefgcgh"; // declare the string system. out. println (str1.indexof ("C"); // find the returned location system. out. println (str1.indexof ("C", 3); // query the return position, starting from 4th. out. println (str1.indexof ("X"); // No-1} is returned }};
Running result:
2, 7,-1

6. Remove spaces.

Trim () can remove spaces between the left and right sides of the string, but spaces in the middle of the string cannot be removed.

Public class stringapidemo06 {public static void main (string ARGs []) {string str1 = "hello"; // defines the string system. out. println (str1.trim (); // output after spaces are removed }};
Running result:
Hello

Seven, character truncation.

To extract part of the content from a string, use the following method:

1) truncate from the specified position to the last position; Public string substring (INT beginindex );

2) intercept the content of the specified range: Public string substring (INT beginindex, int endindex );

Public class stringapidemo07 {public static void main (string ARGs []) {string str1 = "Hello World"; // defines the string system. out. println (str1.substring (6); // captures the system from the first position. out. println (str1.substring (); // intercept 0 ~ Content in five locations }};
Running result:
World
Hello

8. Split the string.

If you want to split a string by the specified string, use: Public String [] Split (string RegEx)

Public class stringapidemo08 {public static void main (string ARGs []) {string str1 = "Hello World"; // defines the string s [] = str1.split (""); // split string by space (INT I = 0; I <S. length; I ++) {// cyclically output system. out. println (s [I]) ;}}
Running result:
Hello
World

The example is split into two strings by space to form a string array containing two strings.

9. case-sensitive conversion.

Convert all uppercase strings to lowercase: Public String touppercase ();

Converts all lowercase strings to University: Public String tolowercase ();

Public class stringapidemo09 {public static void main (string ARGs []) {system. out. println ("Convert \" Hello world \ "to uppercase:" + "Hello World ". touppercase (); system. out. println ("Convert \" Hello world \ "to lowercase:" + "Hello World ". tolowercase ());}
Running result:
Hello World
Hello word

10. Determine whether to start or end with a specified string.

You can use the following methods in string.

1) whether to start with a specified string: Public Boolean starswith (string prefix)

2) Whether to end with a specified string: Public Boolean endwith (string suffix)

Public class stringapidemo10 {public static void main (string ARGs []) {string str1 = "** hello"; // defines the string str2 = "Hello **"; // define the string if (str1.startswith ("**") {// determine whether the system starts. out. println ("(** Hello) starts with **");} If (str2.endswith ("**") {// determines whether the system ends. out. println ("(Hello **) ended ");}}};
Running result:
(** Hello) starts **
(Hello **) ends **

11. case-insensitive string comparison.

public boolean equalsIgnoreCase(String anotherString)
Public class stringapidemo11 {public static void main (string ARGs []) {string str1 = "hello"; // defines the string str2 = "hello"; // defines the string system. out. println ("\" Hello \ "equals \" Hello \ "" + str1.equals (str2); system. out. println ("\" Hello \ "equalsignorecase \" Hello \ "" + str1.20.signorecase (str2); // case-insensitive comparison }};
Running result:
False
Ture

12. Replace the string.

Use this method to complete string replacement:

Public String replaceall (string RegEx, string replacement );

Public class stringapidemo12 {public static void main (string ARGs []) {string STR = "hello"; // defines the string newstr = Str. replaceall ("L", "x"); // replace all l with X system. out. println ("result after replacement:" + newstr );}};
Running result:
Result After replacement: hexxo

 

Common Methods of the string class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.