Java Basic Learning Path (ix) [2]string class common methods

Source: Internet
Author: User

characters and Strings:1. Convert a character array to a string (construction method)
 Public String (charnew-The initial value of the string

2. Convert part of a character array into a string (constructor method)
 PublicString (Char[] Value,intOffset,intcount) Allocates aNewString that contains characters from a subarray of the character array argument.  The offset argument is the index of the first character of the Subarray and the Count argument specifies the length of the Subarray. The contents of the subarray is copied; Subsequent modification of the character array does not affect the newly created string. Parameters:value-Array is the source of Charactersoffset-The initial offsetcount-The lengththrows:indexoutofboundsexception-If The offset and Count arguments index characters outside the bounds of the value array

3. Returns the character corresponding to the specified index (normal method)
CharAt (intchar value at the specified index.

 Public class test1 {    publicstaticvoid  main (String args[]) {        = "Hello";         char c = Str.charat (1);        System.out.println (c);        }} // e

4. Change the string to a character array (normal method)
 This new character array.

 Public classTest1 { Public Static voidMain (String args[]) {string str= "Hello"; Char[] data =Str.tochararray ();  for(inti=0;i<data.length;i++) {Data[i]-=32;//Turn CapitalSystem.out.print (data[i]+ ","); } System.out.println (NewString (data));//Convert a character array to a string    }}
Bytes and Strings

1。 Converting a byte array to a string (construction method)
String (bytenew string by decoding the specified array of bytes using the platform ' s default CharSet.
2. Converting part of a byte array to a string (constructor method)
String (byteintintnew String by decoding the specified subarray of bytes Using the platform ' s default CharSet.
3. Convert a string into byte data (normal)
 Public byte  This this is thedefaultclass byte  Arraysince:jdk1. 1
4. For encoding conversion (normal)
 Public byte[] getBytes (String charsetname)throwsUnsupportedencodingexceptionencodes ThisString into a sequence of bytes using the named CharSet, storing the result into aNew byteArray. The behavior of ThisMethod when ThisString cannot be encoded in the given charset is unspecified. The CharsetencoderclassShould be used if more control over the encoding process is required. Parameters:charsetname-The name of a supported charsetreturns:the resultantbytearraythrows:unsupportedencodingexception-If The named CharSet is not SUPPORTEDSINCE:JDK1.1

string comparison judgment

1.equals (normal), for equal judgment, Case Sensitive

 Public Booleanequals (Object anobject) compares ThisString to the specified object. The result istrue ifand onlyifThe argument is notNULLAnd is a String object that represents the same sequence of characters as Thisobject. Overrides:equals inclassObjectparameters:anobject-The object to compare ThisString againstreturns:true ifThe given object represents a String equivalent to ThisStringfalseotherwisesee Also:compareto (String), Equalsignorecase (String)

2. Make an equal judgment, Case insensitive

 Public Booleanequalsignorecase (String anotherstring) compares ThisString to another string, ignoring CaseConsiderations. Strings considered equal ignoring Case ifThey is of the same length and corresponding characters in the both strings are equal ignoring Case. Characters C1 and C2 are considered the same ignoring Case ifAt least one of the following istrue: The characters is the same (as compared by the==operator) Applying the method Character.touppercase (Char) to each character produces the same resultapplying the method Character.tolowercase (Char) to each character produces the same resultparameters:anotherstring-The String to compare ThisString againstreturns:true ifThe argument is notNULLAnd it represents an equivalent String ignoring Case;falseotherwisesee also:equals (Object)
3. Determine the two character size, (by character encoding comparison)
 public  int   CompareTo (String anotherstring) Specified By:compareto in  interface  comparable<string>parameters:anotherstring - The String to is compared. Returns:the value  0 if  the argument string is equal to this  string; A value less than 0 if  this  string is Lexicographically less than the string argument; and a value greater than 0 if  this   string is lexicographically greater than the string argument.comparetoignorecase  

String Lookup

1.

Java Basic Learning Path (ix) [2]string class common methods

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.