Some function usage methods in the String class

Source: Internet
Author: User

The most commonly used is the length () function, which is to find the lengths of the strings

String s= "";
int I=s.length ();
I result is 0.

If it is a string s=null;
int I=s.length ();
Compile right. Operation will be error

CharAt () function:

The charAt (int index) method is a method that can be used to retrieve a string instance of a character under a specific index. The CharAt () method returns a char value at the specified index position. The index range is 0~length ()-1. For example: Str.charat (0) retrieves the first character in Str, and Str.charat (Str.length ()-1) retrieves the last character.GetChar () function:

GetChars (int srcbegin,int Srcend,char dst[],int dstbegin) copies characters from this string to the target character array.
The first character to copy is at index Srcbegin, and the last character to be copied is at index srcEnd-1 (so the total number of characters to be copied is Srcend-srcbegin). The characters to be copied to the DST sub-array start at index Dstbegin and end at index. For example
String str = "Abcdefghikl";
char[] ch = new CHAR[8];
Str.getchars (2,5,ch,0);
is from the second letter of STR is copied to the fifth, a total of 3 characters, from the first beginning of CH to accept.

replace () function
Replace (char OldChar, char Newchar)
Returns a new string that is passed by using the NewcharReplaceThis string appears in theAllGenerated by OldChar.
 Public class Testreplace {    publicstaticvoid  main (string[] args) {   = " This is my original string, it is very good! " ;    = "at";    = S.replace ("is", r);   System.out.println (s);   } }

toUpperCase () function

The toUpperCase () method is used to convert a string to uppercase. Returns a new string in which all lowercase characters in stringobject are converted to uppercase characters.

var str= "Hello world!" document.write (Str.touppercase ())

toLowerCase ()

The toLowerCase () method is used to convert a string to lowercase. Returns a new string in which all uppercase characters in Stringobject are converted to lowercase characters.

var str= "Hello world!" document.write (Str.tolowercase ())

Trim () function:

The trim () function is to remove the leading and trailing spaces, using trim ("first Quarter earnings") equals "first Quarter earnings"

ToCharArray () function:

String.tochararray method, Function: Converts a string to a character array. ToCharArray is to save the object of each subscript bit of a string object in char[], for example: string a = "ABCD"; Char b[] = A.tochararray (); The information that this b holds is [' a ', ' B ', ' C ', ' d '].

Some function usage methods in 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.