Copy the code code as follows:
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 Programming";
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 returns the number of Unicode code points within 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 some index, different lengths, or both.
//If the characters are different at one or more indexes, assuming k is the minimum value of such indexes, then the return value is the position k of the two strings
//The difference between two char values. If there are no characters with different index positions, the return value is the difference in 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) ignores case and compares string size
System.out.println(strCom.compareToIgnoreCase("I Like PHP"));
//concat(String str) concatenates another string after this string. If the length of the parameter string is 0,
//Return this string, otherwise create a new String object
System.out.println(strCom.equals(strCom.concat("")));
System.out.println(strCom.concat(strCom));
//contains (CharSequence s) determines whether the string contains the specified char value sequence
System.out.println(strCom.contains("JAVA"));
//valueOf(char []data) static method, returns a string containing the characters of the character array
char [] array={'山','东'};
System.out.println(String.valueOf(array));
//valueOf(char[] data,int offset,int count) returns count characters starting from offset in the character array
//String composed of
System.out.println(String.valueOf(array, 0, 1));
//endwith(String suffix) tests whether the 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, otherwise false
System.out.println(strCom.equals("I like JAVA"));
//equalsIgnoreCase(String anotherString) //Ignore case and compare with another string. Note that the parameter type of the equals method is different.
System.out.println(strCom.equalsIgnoreCase("I Like JAva"));
//format(String format,Object ...args) static method uses the specified format string and parameters to return a format string
//%d formatted as decimal integer
//%o formatted as octal integer
//%x %X formatted as 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)
//Format date and time strings by using the given special conversion character as a parameter
//%te day of the month
//%tb specifies the month abbreviation of the locale
//%tB The full name of the month in the specified locale
//%tA specifies the full name of the day of the week in the specified locale
//%ta is the abbreviation for the day of the week in the specified locale
//%tc includes all date and time information
//%tY 4-digit year
//%ty two-digit year
//%tm month
//%tj Day of the year
//%td 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 + "year"+month+"month"+day+"日");
System.out.println(String.format(form, "%tc", date));
//byte[] getBytes() gets the byte sequence 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)
//Get the resulting character sequence of the encoded character set
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)
//Copy 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 of the string. The calculation formula of the hash code of the String object is
//s[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1]
//The hash code of the empty string is 0
System.out.println(strCom.hashCode());
//indexOf(int ch) gets the first index of the character, 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 from the specified index
//fromIndex has no limit. If it is negative, it is equivalent to 0. If it is greater than or equal to the string length, it returns -1
System.out.println(strCom.indexOf('A', 9));
//indexOf(String str)
//indexOf(String str,int fromIndex)
//Returns the index of the first occurrence of the specified string
System.out.println(strCom.indexOf("JAVA"));
//intern() returns the normalized representation of the string object
//When the intern method is called, if the pool already contains a string equal to this String object, return the string in the pool
// Otherwise add this string object to the pool and return this String object reference
//Understanding this processing mechanism also allows us to understand how to save the memory occupied by these 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());
//Same as indexOf, pay attention to the fromIndex parameter, which refers to 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 regular expressions
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 the index offset codePointOffset code points from the given index
System.out.println(strCom.offsetByCodePoints(7, 4));
//Test whether the two string areas are equal. When the first parameter is true, it means ignoring case.
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 string content will be split according to the specified delimiter and stored in the string array. Limit is the number of times the control mode 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)//Determine whether to start with the specified prefix
//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)
//Return a new character sequence
System.out.println(strCom.subSequence(2, 6));
//String substring(int beginindex,int endIndex)
//return substring
System.out.println(strCom.substring(2));
System.out.println(strCom.substring(2, 6));
//toCharArray() converts string into 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) Converts all characters in the string to upper/lower case and returns a new string
System.out.println(strCom.toLowerCase());
System.out.println(strCom.toUpperCase());
System.out.println(strCom.toUpperCase(form));
System.out.println(strCom.toLowerCase(form));
//trim() method removes the leading and trailing blanks of the string
System.out.println((" "+strCom).trim());
//valueOf() static method converts basic data types into strings
System.out.println(String.valueOf(true));
System.out.println(String.valueOf('A'));
System.out.println(String.valueOf(12.0));
}
}