JAVA (String Class)

Source: Internet
Author: User
Tags array to string

Class test{public static void Main (string[] args) {String S1 = "abc"; S1 is a class-type variable, ABC is an object, the maximum character of a string, the initialization of a single can not be changed.  S1 is a reference to a string.  String s2 = new String ("abc");  System.out.println (S1==S2); System.out.println (s1.equals (S2)); The//string class has replicated the Equals method in the object class,//used to determine whether two strings are the same//s1,s2 difference: S1 An object in memory, s  2 has two objects. }} common methods for string types  The string class is useful for describing string things and provides several ways to manipulate strings. Common methods are:  1, get.     1.1 The number of characters contained in the string, that is, the string length. int length ()     1.2 Gets a character on the position based on the position. char charAt (int index)     1.3 Gets the position of the character in the string based on the character.             int indexOf (int ch)//Because the ASCII code is received, the first occurrence of the position in the string is returned by Ch.           int indexOf (int ch,int fromindex)//Get location starting at the specified position.            int indexOf (String str)           int indexOf ( String Str,int fromindex)     Example:  class test{ public static void Method_get ()  {  string str = "ABCDEAKPF"; //Length   SOP (Str.length ()); //Get index   SOP according to characters (Str.indexof (' A ', 3)); If not found return -1 //Get character   SOP according to index (Str.charat (4)); //Reverse lookup character appears position   SOP (Str.lastindexof (' a '));   } public static void Main (string[] args)  {   method_get ();    } public static void sop (Object obj)  {  System.out.prIntln (obj);  }}  2, Judge.     2.1 Whether a string is contained in a string. Boolean contains (str)     2.2 string for content. Boolean isEmpty ()     2.3 Whether the string starts with the specified content. The Boolean startsWith (str);    2.4 String is the end of what.  boolean endsWith (str);    2.5 Determine if the contents of the string are the same Boolean equals (str);    2.6 ignore casing to determine if the content is the same   Boolean equalsignorecase (str);  class test{ public static void Method_is ()  {  String str = " Arraydemo.java ";  SOP (Str.startswith (" Array "));  SOP (Str.endswith (". Java "));  SOP (Str.contains (" (Demo "));  } public static void Main (string[] args)  {  method_is ();  } public static void SOP (Object obj)  {  System.out.println (obj);  }} 3, conversion.     3.1 Convert character array to string          constructor: String (char [])  / string (char [],offeset,c Ount): Character array part to string         static method: static string Copyvalueof (char [])  ,static string copyvalueof ( Char [],offeset, count)      3.2 Convert string to character array             Char [] tochararray ();    3.3 Convert byte array to string             STIRNG (Byte [])     3.4 Convert string to byte array       &NBSP ;       Byte [] getBytes ()     3.5 Converts the base data type to a string: static string valueOf (int/doubule)   with &N Bsp                                 & nbsp   Example:   class test{ public static void Method_trans ()  {  Char [] arr ={' A ', ' B ', ' C ', ' d ', ' e ' , ' F '};  string s = new string (arr);  SOP (s);  string s2 = new String (arr,1,3);  sop (s2);  string S1 = "ZXCVBNM";  char [] CHS = S1.tochararray ();  for (int x=0;x<chs.length;x++)    sop (chs[x]); nbsp;}  public static void Main (string[] args)  {  Method_trans ();  } public static void sop (Object obj )  {  SyStem.out.println (obj);  }}  4, replacing: string replace (Oldchar,newchar) 5, Cut: String Split (Regex) 6, substring: Gets the part of the string. : String substring (BEGIN)/(Begin,end) 7, convert, remove spaces, compare.     7.1 The string is converted to uppercase or lowercase. String toUpperCase ();  string toLowerCase ();    7.2 Removes extra spaces at both ends of the string. String trim ();    7.3 compares the strings in a natural order. int CompareTo (STIRNG);    Exercise 1: Remove both spaces: charat,substring (Begin,end) Exercise 2: Reverse string: ToCharArray () Exercise 3: Count the occurrences of a specified string, int indexOf (STR,INDEX) Exercise 4: Finding the maximum length substring: Two loops, controlling the maximum substring length, determining if there is a Boolean  str.contains (str)// Original and IndexOf similar   Exercise 4 code: public class Test1{  public static void Main (string args [])  {  string a= " SIFDUHSDIFHSAHASIUDHISZHANGXUSHISSSHUIDHIASDFASNDSF ";  String a1=" ASDFSADFZHANGXUSHISSSHUIDKLJFG ";  SOP (Getcommon (A,A1));    }  public static string Getcommon (String str,string str1)  {   for (int x=0;x<str.length (); x + +)   {   for (int y=0;y<=x;y++)    {    String Str2=str1.substring (Y,str1.length ()-x+y);    if (Str.contains (str2))      return str2;11111   } }  return null; } public static void sop (Object c)  {  System.out.println (c);  } }    StringBuffer StringBuffer is a string buffer.   is a container: 1, the length can be changed 2, can directly manipulate a variety of data type 3, will eventually be the ToString method into the string   function: 1, storage          Stringbuffer append (): Adds the specified data as a parameter to the end of the existing data. Returns the original buffer object.         StringBuffer Insert (index, data): You can insert the specified data into the specified position, starting at 0. 2, delete        stringbuffer delate (int start, int end): Contains the header, does not contain the end.         stringbuffer delatecharat (int index): delete specified position element 3, get        char charAt (int index)        int indexOf (string str)        int lastIndexOf (String str)        int length ()        string substring (int start,int end) 4, modify     &NB Sp  stingbuffer replace (int start,int end,string str): Replaces the specified position with the specified string        setcharat (int index, Char c): Specifies the position of the replacement character 5, reversed.        stringbuffer reverse () 6 char [] sb.getchars (int srcbegin,int Srcend,char [],int index from Char [])     StringBuilder  The method is similar to Stringbufffer, and is used basically. The difference is that StringBuffer is thread-synchronized and secure. But always judge the lock, wasting resources. StringBuilder are not threads that are not synchronized. Multithreading is not recommended for use. Upgrade three factors 1, Jianhua write 2, improve efficiency 3, provide security basic data type Object wrapper class  byte byteshort shortint integerlong longboolean booleanfloat Floatdoubl e Doublechar character The object that corresponds to the base data type, the most common function of the class basic data type wrapper class is the conversion between the base data type and the string type. ---;: basic data type + ""; base data type. toString (basic data type), such as: integer.tostring;<---: format: XXX a = xxx.passxxx ("string"); int Number=integer.passint ("123");  string s = strings.valueof (string) public class test1{public static void main (string args []) {SOP (integer.max_value);} public static void Sop (Object c) {System.out.println (c);}}    

JAVA (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.