Java string class methods are parsed in depth

  • 2020-04-01 02:12:52
  • OfStack


import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Locale;
import java.util.Date;
import java.util.regex.PatternSyntaxException;
import javax.xml.crypto.Data;
public class Stringxuexi {
  public static void main(String[] argc)
  {
    //CharAt (int index) returns the Unicode character at index
    String strCom = "JAVA The program design ";
    System.out.println(strCom.charAt(4));

    //CodePointAt (int index) returns the Unicode encoding value of the character at index
    strCom = "I like JAVA ,too";
    System.out.println(strCom.codePointAt(8));

    //CodePointBefore returns the Unicode encoding value of the character at index-1
    System.out.println(strCom.codePointBefore(2));

    //CodePointCount (int beginIndex,int endIndex) method & PI;     Returns the number of Unicode code points in the specified text range
    System.out.println(strCom.codePointCount(0, 3));

    
    //compareTo(String str)
    //If two strings are different, they either have different characters at an index, different lengths, or both
    //If the characters are different at one or more indexes, assuming that k is the minimum of such indexes, the return value is the two strings at position k
    //The difference between two char values, if there are no different index positions for the characters, the return value is the difference between the length of the two strings
    System.out.println(strCom.compareTo("I like PHP"));
    System.out.println(strCom.compareTo("I like JAVA too"));

    //CompareToIgnoreCase (String STR)       Ignore case to compare string size
    System.out.println(strCom.compareToIgnoreCase("I Like PHP"));

    //Concat (String STR) concatenates another String after this String. If the length of the argument String is 0,
    //Returns the String, otherwise a new String object is created
    System.out.println(strCom.equals(strCom.concat("")));
    System.out.println(strCom.concat(strCom));

    //Contains (CharSequence s) determines whether the string contains the specified sequence of char values
    System.out.println(strCom.contains("JAVA"));

    //ValueOf (char []data) static method that returns a string of characters containing an array of characters
    char [] array={' mountain ',' In the east '};
    System.out.println(String.valueOf(array));

    //ValueOf (char[] data,int offset,int count) returns the count characters that contain the array of characters starting from the offset
    //Composed string
    System.out.println(String.valueOf(array, 0, 1));

    //Endwith (String suffix) tests whether a String ends with the specified suffix
    System.out.println(strCom.endsWith("JAVA"));

    //Equals (object obj)       Returns true if the String represented by the given object is equal to this String, or false
    System.out.println(strCom.equals("I like JAVA"));

    //equalsIgnoreCase(String anotherString) // Ignore the case comparison with another string, note the comparison with equals Methods have different parameter types 
    System.out.println(strCom.equalsIgnoreCase("I Like JAva"));

    //The format (String format, the Object... Args) static methods & NBSP;     Returns a format string with the specified format string and parameters
    //%d is formatted as a decimal integer
    //%o is formatted as an octal integer
    //%x %x is formatted as a hexadecimal integer

    System.out.println(String.format("%e %x %o %d %a %% %n", 15.000,15,15,15,15.0));

    
    //format(Locale l,String format,Object ... args)
    //The date and time strings are formatted with the given special converters as arguments
    //%te one day a month
    //% TB specifies the month abbreviation for the locale
    //%tB specifies the full name of the month for the locale
    //%tA specifies the full name of the week for the locale
    //%ta specifies the day of the week for the locale
    //%tc includes all date and time information
    //%tY 4-bit year
    //%ty two bit years
    //% in tm
    //The day of the year
    //%td the day of the month
    Date date = new Date(); 
    Locale form = Locale.CHINA;
    String year = String.format(form, "%tY", date);
    String month    = String.format(form, "%tm", date);
    String day = String.format(form, "%td", date);
    System.out.println(" Today is:  "+ year + " years "+month+" month "+day+" day ");
    System.out.println(String.format(form, "%tc", date));

    //Byte [] getBytes() gets the byte sequence & PI of the string;    
    byte[] str = strCom.getBytes();
    for (int i = 0;i < str.length;i++)
      System.out.print(str[i]+" ");

    //getBytes(Charset charset)
    //getBytes(String string)
    //The resulting character sequence of the coded character set is obtained
    try {
      str = strCom.getBytes(Charset.defaultCharset());
      for (int i = 0; i < str.length; i++)
        System.out.println(str[i] + " ");
    } catch (UnsupportedCharsetException e) {
      // TODO: handle exception
      e.printStackTrace();
    }

    //getchars ( int srcBegin,int srcEnd,char[] dst,int dstBegin)
    //Copies the characters from this string to the target character array
    char[] dst = new char[10];
    strCom.getChars(0, 10, dst, 0);
    for (int i = 0; i < dst.length;i++)
      System.out.print(dst[i]);
    System.out.println();

    //HashCode ()       Returns the hash code for the String. The hash code for the String object is
    //s[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1] 
    //The hash code of an empty string is 0
    System.out.println(strCom.hashCode());

    //IndexOf (int ch) gets the first indexOf a character, the ch is a character, if not, returns -1
    System.out.println(strCom.indexOf('A'));

    //indexOf(int ch,int fromIndex)    // Returns the index of the specified character starting at the specified index 
    //FromIndex is unbounded, equal to 0 if negative, and returns -1 if greater than or equal to the string length
    System.out.println(strCom.indexOf('A', 9));

    //indexOf(String str)
    //indexOf(String str,int fromIndex)
    //Returns the index of the specified string where the string first appears
    System.out.println(strCom.indexOf("JAVA"));

    //Intern ()       Returns a normalized representation of the string object
    //When the intern method is called, if the pool already contains a String equal to the String object, the String in the pool is returned
    //Otherwise add the String object to the pool and return the String object reference
    //Understanding this processing mechanism also allows us to learn how to save memory for strings when using string constants.
    String strCom2 = new String("I like JAVA");
    System.out.println(strCom == strCom2);
    System.out.println(strCom.endsWith(strCom2));
    System.out.println(strCom.compareTo(strCom2));
    System.out.println(strCom.intern() == strCom2.intern());
    String s1 = new String(" hello ,Java Free man ");
    String s2 = new String(" hello ,") + "Java Free man ";
    System.out.println(s1==s2);
    System.out.println(s1.intern()==s2.intern());

    //With indexOf, notice the fromIndex parameter, which is the reverse search from fromIndex
    System.out.println(strCom.lastIndexOf('A'));
    System.out.println(strCom.lastIndexOf('A',10));
    System.out.println(strCom.lastIndexOf("JAVA"));
    System.out.println(strCom.lastIndexOf("JAVA", 10));

    //Return string length
    System.out.println(strCom.length());

    //Matchs (String regex) matches a regular expression
    try {
      String regex = "1234";
      System.out.println(regex.matches("//d{4}"));
      System.out.println(regex.replaceAll("//d{4}", "chen"));
      System.out.println(regex.replaceFirst("//d{4}", "chen"));
    } catch (PatternSyntaxException e) {
      // TODO: handle exception
      e.printStackTrace();
    }

    // offsetByCodePoints(int index,int codePointOffset)
    //Returns an index that offsets codePointOffset code points from a given index
    System.out.println(strCom.offsetByCodePoints(7, 4));

    //Tests that the two string regions are equal, ignoring case when the first parameter is true
    System.out.println(strCom.regionMatches(true, 0, "I lIke", 0, 3));
    System.out.println(strCom.regionMatches(0, "I like", 0, 3));

    System.out.println(strCom.replace('A', 'a'));
    System.out.println(strCom.replace("JAVA", "PHP"));

    //String[] split(String regex,int limit)
    //The specified delimiter splits the string contents into an array of strings, and limit controls the number of times the pattern is applied
    String[] info = strCom.split(" ,");
    for (int i = 0; i < info.length;i++)
      System.out.println(info[i]);

    info    = strCom.split(" ", 2);
    for (int i = 0; i < info.length;i++)
      System.out.println(info[i]);

    //startsWith(String prefix,int toffset)// Determines whether to start with the specified prefix 
    //The toffset is negative or greater than the string length and the result is false
    System.out.println(strCom.startsWith("I"));
    System.out.println(strCom.startsWith("I",-1));

    //CharSequeuece subSequeuece(int beginIndex,int endIndex)
    //Returns a new sequence of characters
    System.out.println(strCom.subSequence(2, 6));

    //String substring(int beginindex,int endIndex)
    //Returns a substring
    System.out.println(strCom.substring(2));
    System.out.println(strCom.substring(2, 6));

    //ToCharArray () string to character array
    char[] str1 = strCom.toCharArray();
    for (int i = 0; i < str1.length;i++)
      System.out.print(str1[i]+" ");
    System.out.println();

    //ToLowerCase (Locale Locale) returns a new string by making all characters in the string large/small
    System.out.println(strCom.toLowerCase());
    System.out.println(strCom.toUpperCase());
    System.out.println(strCom.toUpperCase(form));
    System.out.println(strCom.toLowerCase(form));

    //The trim() method fetches the before and after whitespaces of the string
    System.out.println(("    "+strCom).trim());

    //The valueOf ()         The static method implements the conversion of basic data types to strings
    System.out.println(String.valueOf(true));
    System.out.println(String.valueOf('A'));
    System.out.println(String.valueOf(12.0));
  }
}


Related articles: