Java-string Basic article

Source: Internet
Author: User

One, string strings understand

A Java string class that contains the value of a string and some methods for implementing string-related operations

1. String strings can be divided into static strings and dynamic strings

Static initialization string:string s1 = "Hello";

Dynamic initialization string: strings2 = new string ("Hello");

2 . The difference between static initialization and dynamic initialization

String s = "Hello"; Allocating space in a string constant pool (static)string s = string ("Hello");//Allocate space in heap memory ( dynamic ) Second, string string common methods

charAt (int index)//returns the character with the specified subscript ( zero-based )

CompareTo (string s)// string comparison concat (string s)//string contains/stitching equals () //Determines whether the contents of two strings are the same

GetBytes ()//return byte array

indexOf ()//Find sub-string subscript Split ()//split string returns an array of strings with the StringTokenizer function "common" subString ()//Get sub-stringtoString ()//object converted to uppercase lettertoLowerCase ()//convert to lowercase letter  examples are as follows:

Conversion of strings and character arrays

 Public classstring001 { Public Static voidMain (string[] args) {String str1= "Xiaoluo";//Define a string        CharC[] = Str1.tochararray ();//ToCharArray: Converts the preceding name string to an array         for(inti=0;i<c.length;i++) {System.out.print (C[i]+ "\ T");//loops out each element in the array} System.out.println ("");//line BreakString str2 =NewString (c);//convert all arrays to a stringString STR3 =NewString (c,0,3);//convert 0-3 elements to a stringSystem.out.println (STR2);    System.out.println (STR3); }}//x i a o l u o//Xiaoluo//Xia

Remove the character from the specified position in the string  

 Public class String002 {    public  staticvoid  main (string[] args)    {         // Declare a string        System.out.println (Str1.charat (3));   Charat Method: Remove the element labeled 3     }}//o

Converts a string into a byte array, while turning a byte array into a string

A string can be transformed into a byte array through the GetBytes () method, and a byte array can then be converted to a string by constructing a string

 Public classString003 { Public Static voidMain (string[] args) {String str1= "Beijing";//declares a string        byteB[] =str1.getbytes ();//by GetBytes: Converting a string to a byte arraySystem.out.println (NewString (b));//converts an entire byte array to a stringSystem.out.println (NewString (b,1,3));//converts an element labeled 1-3 in byte array B to a string    }}//Beijing//Eij

Gets the length of a string

Gets the length of the string by calling the length method

 Public class stringlength {    publicstaticvoid  main (string[] args)    {         // declares a string        System.out.println ("str1 string length is:" +str1.length ()); // get the length of a string by length     }}//str1 string length is: 7

Note: length and length () difference?

First make it clear that the call to any method must be followed by a () is the implementation, and length is the length of the array, the element has no () parentheses, and the string call length is a method, the call of the method must be preceded with ()

The IndexOf method finds whether an element exists in the specified string

 Public classString_indexof { Public Static voidMain (string[] args) {String str1= "Qwertyuioepe";//declares a stringSystem.out.println (Str1.indexof ("w"));//find where w elements exist in a string by using the IndexOf methodSystem.out.println (Str1.indexof ("E", 4));//find out whether the e element exists and find the first e from the fourth element by using the IndexOf methodSystem.out.println (Str1.indexof ("B"));//elements that do not exist in the string return-1        }}//1//9//-1

Remove both left and right spaces

Trim () method to remove extra spaces in a string

 public  class   String_indexof { public  static  void   main (string[] args) { String str1  = "Qwertyuioepe"; //             declares a string  System.out.println (Str1.trim ()); // trim method: Strips the left and right spaces in a string          System.out.println (STR1); }}  // qwertyuioepe  //  Qwertyuioepe   

String interception

Intercepting an element between a specified string or string by using the substring () method

 Public classString_indexof { Public Static voidMain (string[] args) {String str1= "Qwertyuioepe";//declares a stringSystem.out.println (Str1.trim ());//Trim Method: Removes whitespace from left and right sides of the string//System.out.println (str1);System.out.println (Str1.substring (4));//substring method: element After starting from four elementsSystem.out.println (str1.substring (0,4));//The result is labeled as an element between 0-4        }}//Qwertyuioepe//Tyuioepe//qwe

Splits a string by a specified string

In Stringzing, the split () method allows splitting of the string, and the split data is returned as an array of Yui strings, as follows: splitting by spaces and saving the split string in a list

 Public classString_indexof { Public Static voidMain (string[] args) {String str1= "Hello World";//declares a stringString str2[] = Str1.split ("");//splits a string in str1 by space and saves the split element in a list             for(inti=0;i<str2.length;i++)//loop out all elements in str2[]{System.out.println (str2[i]); }        }}//Hello//World 

To judge a string to end with a certain beginning or a

StartsWith: Judging string starts with XXX

EndsWith: Judging string to end with XXX

 Public classString_indexof { Public Static voidMain (string[] args) {String str1= "***hello World";//declares a stringString str2 = "nihao***"; if(Str1.startswith ("* * *") {System.out.println ("(***hello World) begins with * * * *"); }            if(Str2.endswith ("* * *") {System.out.println ("(nihao***) ends with * * *"); }        }}//(***hello World) begins with * * *//(nihao***) End With * * *

Specify a string to replace

Use ReplaceAll in string to specify line substitution for characters that exist in strings

 Public class String_indexof {        publicstaticvoid  main (string[] args) {            = " Hello World "; // declares a string            String str2 = Str1.replaceall ("W", "w"); // ReplaceAll Method: Replace W element with W             System.out.println (str2);        }} // Hello World

Java-string Basic 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.