Some methods of the string class for Java

Source: Internet
Author: User
Tags chr

First, get the information about the string object

1. Get the length of the string by calling the length () method.

String Str= "This is a String";

int Len =str.length ();

The capacity () method of the 2.StringBuffer class is similar to the length () method of the string class, but she tests the size of the memory space allocated to StringBuffer, not the memory space currently being used.

3. You can use the IndexOf () and LastIndexOf () methods if you want to determine the position of a specified character or substring in a string in a given string.

String Str= "This is a String";

Int index1 =str.indexof ("i");   index=2

Int index2=str.indexof (' i ', index+1);   Index2=5

Int index3=str.lastindexof ("I");   Index3=15

Int index4=str.indexof ("String");  index4=10

Ii. comparison and manipulation of String objects

1. Comparison of String objects

The Equals () method of the string class is used to determine whether two strings are equal.

String Str= "This is a String";

Boolean Result=str.equals ("This is another String");

Result=false

2. Access to a String object

A, Method Charat () to get the character at the specified position.

String Str= "This is a String";

Char Chr=str.charat (3); Chr= "I"

B, Method GetChars () to get a part of the string string

public void GetChars (int srcbegin,int srcend,char[]dst,int dstbegin)

String Str= "This is a String";

Char chr =new char[10];

Str.getchars (5,12,chr,0);  Chr= "is a St"

C, SubString () is another way to extract a string that specifies where to begin extracting the string and where to end it.

3. Manipulating strings

The A, replace () method can replace one character in a string with another character.

String Str= "This is a String";

String str1=str.replace (' t ', ' t '); Str1= "This is a String"

The B, concat () method can combine two strings into a single string.

String Str= "This is a String";

String str1=str.concat ("Test"); Str1= "This is a String Test"

The C, toUpperCase (), and toLowerCase () methods implement string-case conversions, respectively.

String Str= "This is A STRING";

String str1=str.tolowercase (); Str1= "This is a string";

The D, Trim () method removes the spaces at the beginning and end of the string.

String str= "This is a string   “;

String Str1=str.trim ();   Str1= "This is a String"

The E, String class provides a static method, valueof (), that converts any type of data object to a string. Such as

System.out.println (string,valueof (MATH,PI));

Some methods of the string class for Java

Related Article

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.