Common methods of the string class

Source: Internet
Author: User

    1. String and character


Method name Method type Method description
Public String (char[] value) Construction method Converts the specified array of characters into a string
Public String (char[] value,int offset,int count) Construction method Converts a character array of a specified range into a string
public char charAt (int index) Common methods Gets the character at the specified position within the string
Public Char ToCharArray () Common methods To change a string into a character array
Example: The character that gets the specified index position


public class Demo

{

public static void Main (string[] args) {

String str = "Hello world";

Char c =str.charat (2);

System.out.println (c);

}

}

Example: manipulating strings to and from character arrays

public class Demo

{

public static void Main (string[] args) {

String str = "Hello";

char[] data = Str.tochararray ();//Converts a string into a character array

for (int i =0;i<data.length;i++) {

Data[i]-= 32;

}

System.out.println (new String (data));

}

}

Example : Give a string that asks whether the string is composed of numbers.

public class Demo

{

public static void Main (string[] args) {

String msg = "235468841";

System.out.println (Isnumber (msg));

}

public static Boolean isnumber (String str) {

char[] data = Str.tochararray ();//Converts a string into a character array

for (int i=0;i<data.length;i++) {

if (data[i]> ' 9 ' | | data[i]< ' 0 ') {

return false;

}

}

return true;

}

}

2. Strings and bytes


Method name Method type Method description
Public String (byte[] bytes) Construction method Change a byte array to a string
Public String (byte[] bytes,int offset,int length) Construction method Converts a byte array of a specified range into a string
Public byte[] GetBytes () Common methods Changing a string into a byte array
Public byte[] GetBytes (String charname) Common methods Encoding Conversion
Example:Implementing string-to-byte conversions


public class Demo{public static void Main (string[] args) {string msg = "HelloWorld"; byte data[] = Msg.getbytes ();//change String to byte Array for (int i=0;i<data.length;i++) {System.out.println (data[i]);d ata[i]-= 32;} System.out.println (); System.out.println (new String (data));}}

3. String comparisons

Method name Method type Method description
public boolean equals (String str) Common methods Determines whether two strings are equal (case sensitive)
public boolean equalsignorecase (String str) Common methods Determines whether two strings are equal (case insensitive)
public int compareTo (String str) Common methods Compare uppercase relationships of two strings

Example: observing two types of string comparisons


public class demo{

public static void Main (string[] args) {

String msg = "HelloWorld";

String msgb= "HELLOWORLD";

System.out.println (Msg.equals (MSGB));//false

System.out.println (Msg.equalsignorecase (MSGB));//true

}


}

4. String Lookup

Method name Method type Method description
Public boolean contains (String str) Common methods Determine if a string exists
public int indexOf (String str) Common methods Finds the position of the specified character from scratch, returns the position of the specified character if found, otherwise, returns 1
public int indexOf (String str,int fromIndex) Common methods The position is returned by the specified position by the front and back, if found, returns the location of the character, otherwise, 1
public int lastIndexOf (String str) Common methods


Finds the position of the specified character from the back forward

public int lastIndexOf (String str,int fromIndex) Common methods From the specified position, from the back forward to find the location
public boolean startsWith (String prefix) Common methods Determines whether to start with the specified string


public boolean startsWith (String prefix,int toffset)

Common methods Starts at the specified position to determine whether to start with the specified string
public boolean endsWith (String suffix) Common methods Determines whether to end with the specified string

Example: using contains () query

public class demo{

public static void Main (string[] args) {

String str = "Hello world";

if (Str.contains ("Hello")) {

SYSTEM.OUT.PRINTLN ("The specified string has been found");

}

}


}

5. String substitution

Method name Method type Method description
public string ReplaceAll (string regex,string replacement) Common methods Full replacement
public string Replacefirst (string regex,string replacement) Common methods Replace the first

Example: Replace operation

public class demo{

public static void Main (string[] args) {

String str = "Hello world";

System.out.println (Str.replaceall ("L", "_"));

}


}

6. String splitting

Method name Method type Method description
Public string[] Split (String regex) Common methods Full split
Public string[] Split (String regex,int limit) Common methods Split part

Example: implementing a split of IP addresses

public class demo{

public static void Main (string[] args) {

String str = "192.168.1.1";

string[] data = str.split ("\ \");

for (int i =0;i<data.length;i++) {

System.out.println (Data[i]);

}

}


}

7. String interception

Method name Method type Method description
Public String substring (int beginindex) Common methods Intercept from start position to end
Public String substring (int beginindex,int endIndex) Common methods Set Start and End indexes

8. Other methods

Method name Method type Method description
public string concat (String str) Common methods String connection
Public String Intern () Common methods string into Pool
public boolean isEmpty () Common methods Determines whether an empty string
public int Length () Common methods Get string length
Public String toLowerCase () Common methods


Turn lowercase

Public String toUpperCase () Common methods


Turn capital

Public String trim () Common methods Remove left and right spaces































This article is from the "10281302" blog, please be sure to keep this source http://10291302.blog.51cto.com/10281302/1691957

Common methods for string classes

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.