Returns the String type for JSON.

Source: Internet
Author: User
String: String type 1. constructor String (byte [] bytes): constructs a String object through a byte array. String (char [] value): constructs a String object using a char array. String (Stingoriginal): constructs an original copy. That is,... SyntaxHi

String: String type

1. Constructor
String (byte [] bytes): constructs a String object through the byte array.
String (char [] value): constructs a String object using a char array.
String (Sting original): constructs an original copy. Copy an original file.
String (StringBuffer buffer): Constructs String objects through the StringBuffer array.
For example:
Byte [] B = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J '};
Char [] c = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 '};
String sb = new String (B); // abcdefghij
String sb_sub = new String (B, 3, 2); // de
String SC = new String (c); // 0123456789.
String SC _sub = new String (c, 3, 2); // 34
String sb_copy = new String (sb); // abcdefghij
System. out. println ("sb:" + sb );
System. out. println ("sb_sub:" + sb_sub );
System. out. println ("SC:" + SC );
System. out. println ("SC _sub:" + SC _sub );
System. out. println ("sb_copy:" + sb_copy );
Output result: sb: abcdefghij
Sb_sub: de
SC: 0123456789
SC _sub: 34 // www.heatpress123.net
Sb_copy: abcdefghij

Ii. Method:

Note: ①. All methods are public.
②. Writing format: [modifier] <返回类型> <方法名([参数列表])>

Example: static int parseInt (String s)
It indicates that this method (parseInt) is a class method (static), the return type is (int), and the method must be of the String type.

1. char charAt (int index): Take a character in the string. The index parameter indicates the ordinal number in the string. The ordinal number of a string starts from 0 to length ()-1.
Example: String s = new String ("abcdefghijklmnopqrstuvwxyz ");
System. out. println ("s. charAt (5):" + s. charAt (5 ));
Result: s. charAt (5): f
2. int compareTo (String anotherString): The current String object is compared with anotherString. If the two strings are not equal, 0 is returned. If the two strings are not equal, the first character difference is returned. In another case, the front part of a long string happens to be a short string and Returns their length difference.
3. int compareTo (Object o): If o is a String Object, it has the same function as 2; otherwise, a ClassCastException is thrown.
Example: String s1 = new String ("abcdefghijklmn ");
String s2 = new String ("abcdefghij ");
String s3 = new String ("abcdefghijalmn ");
System. out. println ("s1.compareTo (s2):" + s1.compareTo (s2); // return the length difference
System. out. println ("s1.compareTo (s3):" + s1.compareTo (s3); // return the difference between 'K' and 'A '.
Result: s1.compareTo (s2): 4
S1.compareTo (s3): 10
4. String concat (String str): connects the String object with str.
5. boolean contentEquals (StringBuffer sb): Compares the String object with the StringBuffer object sb.
6. static String copyValueOf (char [] data ):
7. static String copyValueOf (char [] data, int offset, int count): These two methods convert the char array to a String, similar to one of the constructors.
8. boolean endsWith (String suffix): whether the String object ends with suffix.
Example: String s1 = new String ("abcdefghij ");
String s2 = new String ("ghij ");
System. out. println ("s1.endsWith (s2):" + s1.endsWith (s2 ));
Result: s1.endsWith (s2): true
9. boolean equals (Object anObject): If anObject is not empty and is the same as the current String Object, true is returned. Otherwise, false is returned.
10. byte [] getBytes (): converts the String object to a byte array.
11. void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin): This method copies the string to the character array. SrcBegin indicates the start position of the copy, srcEnd indicates the end position of the copy, dst indicates the destination character array, and dstBegin indicates the start position of the copy of the target character array.
For example, char [] s1 = {'I', '', 'l', 'O', 'V', 'E','', 'h ', 'E', 'R ','! '}; // S1 = I love her!
String s2 = new String ("you! "); S2.getChars (0, 3, s1, 7); // s1 = I love you!
System. out. println (s1 );
The result is: I love you! // Www. software8.co
12. int hashCode (): return the hash table code of the current character.
13. int indexOf (int ch): finds only the first matching character position.
14. int indexOf (int ch, int fromIndex): locate the first matched character position from fromIndex.
15. int indexOf (String str): finds only the first matching String position.
16. int indexOf (String str, int fromIndex): locate the first matched String starting from fromIndex.
Example: String s = new String ("write once, run anywhere! ");
String ss = new String ("run ");
System. out. println ("s. indexOf ('R'):" + s. indexOf ('R '));
System. out. println ("s. indexOf ('R', 2):" + s. indexOf ('R', 2 ));
System. out. println ("s. indexOf (ss):" + s. indexOf (ss ));
Result: s. indexOf ('R'): 1
S. indexOf ('R', 2): 12
S. indexOf (ss): 12
17. int lastIndexOf (int ch)
18. int lastIndexOf (int ch, int fromIndex)
19. int lastIndexOf (String str)
20. int lastIndexOf (String str, int fromIndex) the above four methods are similar to the Methods 13, 14, 15, and 16. The difference is: Find the last matched content.
Public class CompareToDemo {
Public static void main (String [] args ){
String s1 = new String ("acbdebfg ");

System. out. println (s1.lastIndexOf (int) 'B', 7 ));
}
}
Running result: 5
(The fromIndex parameter is 7, which is the number of digits starting from the last character g of the string acbdebfg. It is used to start matching from character c and find the position where the last matching B is located. So the result is 5)


21. int length (): returns the length of the current string.
22. String replace (char oldChar, char newChar): replace the first oldChar in the String with newChar.
23. boolean startsWith (String prefix): determines whether the String object starts with a prefix.
24. boolean startsWith (String prefix, int toffset): determines whether the String object starts with a prefix from the toffset position.
Example: String s = new String ("write once, run anywhere! ");
String ss = new String ("write ");
String sss = new String ("once ");
System. out. println ("s. startsWith (ss):" + s. startsWith (ss ));
System. out. println ("s. startsWith (sss, 6):" + s. startsWith (sss, 6 ));
Result: s. startsWith (ss): true
S. startsWith (sss, 6): true
25. String substring (int beginIndex): obtains the substring from the beginIndex position to the end.
26. String substring (int beginIndex, int endIndex): Obtain the substring from beginIndex to endIndex.
27. char [] toCharArray (): converts the String object to a char array.
28. String toLowerCase (): converts a String to lowercase.
29. String toUpperCase (): converts a String to uppercase.
Example: String s = new String ("java. lang. Class String ");
System. out. println ("s. toUpperCase ():" + s. toUpperCase ());
System. out. println ("s. toLowerCase ():" + s. toLowerCase ());
Result: s. toUpperCase (): JAVA. LANG. CLASS STRING
S. toLowerCase (): java. lang. class string
30. static String valueOf (boolean B)
31. static String valueOf (char c)
32. static String valueOf (char [] data)
33. static String valueOf (char [] data, int offset, int count)
34. static String valueOf (double d)
35. static String valueOf (float f)
36. static String valueOf (int I)
37. static String valueOf (long l)
38. static String valueOf (Object obj)
The above method is used to convert different types into Java compiler type. These are all class methods.

 

 

Common Methods of the String class in Java:

Public char charAt (int index)
Returns the index character in the string;
Public int length ()
Returns the length of the string;
Public int indexOf (String str)
Returns the position where str appears for the first time in the string;
Public int indexOf (String str, int fromIndex)
Returns the position where str appears for the first time from fromIndex;
Public boolean inclusignorecase (String another)
Compares the string with another (Case Insensitive );
Public String replace (char oldchar, char newChar)
Replace the oldChar character with the newChar character in the string
Public boolean startsWith (String prefix)
Determines whether the string starts with a prefix string;
Public boolean endsWith (String suffix)
Determines whether a string ends with a suffix string;
Public String toUpperCase ()
Returns an uppercase string;
Public String toLowerCase ()
Returns a string in lowercase.
Public String substring (int beginIndex)
Returns the child string from beginIndex to the end;
Public String substring (int beginIndex, int endIndex)
Returns the substring starting from beginIndex to ending with endsIndex.
Public String trim ()
Returns the string that removes the leading and trailing spaces from the string.
Public String [] split (String regex)
Returns a string array separated by a specified separator.
Instance:
Public class SplitDemo {
Public static void main (String [] args ){

String date = "2008/09/10 ";
String [] dateAfterSplit = new String [3];
DateAfterSplit = date. split ("/"); // use "/" as the separator to split the date string and put the result into three strings.

For (int I = 0; I System. out. print (dateAfterSplit [I] + "");
}
}

Running result: 2008 09 10 // The result is the split three strings.

Instance:
TestString1.java:
Program code
Public class TestString1
{
Public static void main (String args []) {
String s1 = "Hello World ";
String s2 = "hello world ";
System. out. println (s1.charAt (1 ));
System. out. println (s2.length ());
System. out. println (s1.indexOf ("World "));
System. out. println (s2.indexOf ("World "));
System. out. println (s1.equals (s2 ));
System. out. println (s1.20.signorecase (s2 ));

String s = "I Am a J2EE programmer ";
String sr = s. replace ('I', 'you ');
System. out. println (sr );
}
}

TestString2.java:
Program code

Public class TestString2
{
Public static void main (String args []) {
String s = "Welcome to Java World! ";
String s2 = "magci ";
System. out. println (s. startsWith ("Welcome "));
System. out. println (s. endsWith ("World "));
String sL = s. toLowerCase ();
String sU = s. toUpperCase ();
System. out. println (sL );
System. out. println (sU );
String subS = s. substring (11 );
System. out. println (subS );
String s1NoSp = s2.trim ();
System. out. println (s1NoSp );
}

 

Related 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.