Summary of common USES of String in Java

  • 2020-04-01 02:24:19
  • OfStack

1 > To obtain
 
  1.1: the number of characters contained in a string, that is, the length of the string.
  Int length(): gets the length

  1.2: get a character in a position according to 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): returns the first occurrence of the ch in the string.
  Int indexOf(int ch,int fromIndex): gets the position where the ch appears in the string, starting with the position specified by fromIndex.

  Int indexOf(String STR): returns the first occurrence of STR in a String.
  Int indexOf(String STR, int fromIndex): gets the position where STR appears in the String, starting with the position specified by fromIndex.

  1.4: int lastIndexOf(String STR): reverse index.


2 > judge

  2.1: whether a substring is included in the string.
          Boolean contains (STR);
    Special: indexOf(STR): can index STR until it appears for the first time. If it returns -1, it does not exist in the string.
                        Therefore, it can also be used to determine whether to include a given.
            If (STR) indexOf (" a ")! = 1)

            And this method can not only determine, but also obtain the location of the occurrence.

  2.2: whether there is content in the string.
            Boolean isEmpty(): the principle is to determine if the length is 0.

  2.3: whether the string begins with the specified content.
    Boolean startsWith (STR);

  2.4: whether the string ends with the specified content.
    Boolean endsWith (STR);

  2.5: to determine whether the character content is the same, the equals method in the object class is copied.
    Boolean equals (STR);

  2.6: determine if the content is the same and ignore case.
  Boolean. EqualsIgnorecase ();

3 > conversion
 
  3.1: converts an array of characters into a string.
  Constructor: String(char[])
          String(char[],offset,count): converts a portion of the array of characters into a String
    Static method:
        The static String copyValueOf (char []);
        Static String copyValueOf(char[] data,int offset,int count);

        The static String the valueOf (char []);

  3.2: converts a string to a group of characters
  Char [] tocharArray ();

  3.3: converts a byte array into a string.
          String (byte [])
          String(byte[],offset,count): converts a portion of the byte array into a String

  3.4: converts a string to a byte array.
  Byte [] getBytes ()

  3.5: converts basic data types into strings,
  The static String the valueOf (int)
  The static String the valueOf (double)

  // 3+"" is the same value as string.valueof (3)
  Special: strings and byte arrays can be encoded during conversion.

4 > replace
  String replace (oldchar, newchar);

5 > cutting
  The split String [] (regex);

6 > The substring. Gets a portion of a string
  String subString (begin);
  String subString (begin and end);

7 > Convert, remove Spaces, compare.
 
  7.1: converts a string to uppercase or lowercase
    String toUpperCsae() large to small
    String toLowerCsae() turns small to large

  7.2: removes multiple Spaces from both ends of the string
    String trim ();

  7.3: compare the two strings in their natural order
    Int compareTo (string);

  Please take a look at the following code. The following codes are all for the above seven USES of string.


class StringMethodDemo 
{
 public static void method_Zhuanhuan_Qukong_Bijiao()
 {
  String s = "     hello Java    ";

  //The print result is :(both hello and Java front and back doors have Spaces) hello Java
  sop(s.toUpperCase());
  //The print result is :(both HELLO and JAVA front and back doors have Spaces) HELLO JAVA
  sop(s.toLowerCase());
  //Print and the result: "hello Java" without Spaces
  sop(s.trim());
  //Capital of the comparison number, print: 1, because b corresponds to the ASCII value of 98,
  //A corresponds to 97, so b minus a is equal to 1
  String s1 = "abc";
  String s2 = "aaa";
  sop(s1.compareTo(s2));
 }
 public static void method_sub()
 {
  String s = "abcdef";
  //The print result is: cdef, starting at the specified location and ending at the specified location. If the corner mark does not exist, the string corner mark overbounds.
  sop(s.substring(2));
  //The print result is: CD, including the head, not the tail.
  sop(s.substring(2,4));
 }
 public static void method_split()
 {
  String s = "zhangsan,lisi,wangwu";
  String[] arr = s.split(",");
  for(int x=0; x<arr.length; x++)
  {
   sop(arr[x]);
  }
 }
 public static void method_replace()
 {
  String s = "hello java";
  //String s1 = s.replace('a','n');
  //String s1 = s.r eplace (' w ', 'n');   If the character to be replaced does not exist, the original string is returned

  String s1 = s.replace("java","world");//The print result is: hello world
  sop("s="+s); //The print result is: hello Java because once the string is initialized, the value cannot be changed
  sop("s1="+s1);//The print result is: hello JNVN
 }
 public static void method_trans()
 {
  char[] arr = {'a','b','c','d','e','f'};
  String s = new  String(arr,1,3);
  sop("s="+s);//The print result is: BCD
  String s1 = "zxcvbnm";
  char[] chs = s1.toCharArray();
  for(int x=0; x<chs.length; x++)
  {
   sop("ch="+chs[x]);//The print result is: ch=z, x,c,v,b,n,m
  }
 }
 public static void method_is()
 {
  String str = "ArrayDemo.java";
 //Determines if the file name is the beginning of the Array word
  sop(str.startsWith("Array"));

 //Determines if the file name is a.java file
  sop(str.endsWith(".java"));

 //Determines whether Demo is included in the file
  sop(str.contains("Demo"));
 }
 
 public static void method_get()
 {
  String str = "abcdeakpf";
  //The length of the
  sop(str.length());
  //Gets a character by index
  sop(str.charAt(4));
  //SOP (STR) charAt (40)); When accessing to the Angle of the string does not exist in the StringIndexOutOfBoundsException happens (string Angle the cross-border exception)
  //Gets the index by character
  //sop(str.indexOf('a'));
  sop(str.indexOf('a',3));//It's 5, because 3 is d,
        //So we're going to look for a after d, and the fifth Angle is a
  //SOP (STR. IndexOf ('t',3)) print: -1, return -1 if no corner mark is found
  
  //Reverse index the position where a character appears (look from right to left, but the corner marks still start from left)
  sop(str.lastIndexOf("a"));
 }
 public static void main(String[] args) 
 {
   method_Zhuanhuan_Qukong_Bijiao();
  //method_sub();
  //method_split();
  //method_replace();  
  //method_trans(); 
  //method_is();
  //method_get();
  /*
  String s1 = "abc";
  String s2 = new String("abc");
  String s3 = "abc";
  System.out.println(s1==s2);
  System.out.println(s1==s3);
  */
 }
 public static void sop(Object obj)
 {
  System.out.println(obj);
 }
}


Related articles: