use of the Java-string class
Directory
Use of the Java-string class using a directory string class understand 1 String class overview 2 construction method String's use code example string's feature cannot be changed once it is assigned
1 Use of the String Class (understanding) 1.1 String class overview
A string is a string of data (character sequences) that consists of multiple characters that
can be viewed as an array of characters
1.2 Construction Methodspublic string (): Empty construct public string (byte[] bytes): Convert a byte array to a string public string (byte[] bytes,int index,int length): To convert a portion of a byte array to a string public string (char[] value): Convert a character array to a string public string (char[] Value,int index,int count): Turn a part of a character array into a string public string (string original): Converting a string constant value to a string byte array character array string
2 String Usage code example
public class Stringdemo {public static void main (string[] args) {//public string (): Empty construct string S1 =
New String ();
System.out.println ("S1:" + S1);
System.out.println ("S1.length ():" + s1.length ());
System.out.println ("--------------------------");
public string (byte[] bytes): Convert byte array to string byte[] Bys = {97, 98, 99, 100, 101};
String s2 = new string (bys);
System.out.println ("s2:" + s2);
System.out.println ("S2.length ():" + s2.length ());
System.out.println ("--------------------------"); public string (byte[] bytes,int index,int length): Turn a part of a byte array into a string//I want to get the string, "BCD" string s3 = new String (
Bys, 1, 3);
System.out.println ("S3:" + S3);
System.out.println ("S3.length ():" + s3.length ());
System.out.println ("--------------------------");
public string (char[] value): Convert a character array to a string char[] chs = {' A ', ' B ', ' C ', ' d ', ' e ', ' love ', ' Lin ', ' Pro '}; String S4 = new string (CHS);
System.out.println ("S4:" + S4);
System.out.println ("S4.length ():" + s4.length ());
System.out.println ("--------------------------");
public string (char[] Value,int index,int count): Convert part of a character array to string s5 = new String (CHS, 2, 4);
System.out.println ("S5:" + S5);
System.out.println ("S5.length ():" + s5.length ());
System.out.println ("--------------------------");
public string (string original): Convert string constant value to string s6 = new String ("ABCDE");
System.out.println ("S6:" + S6);
System.out.println ("S6.length ():" + s6.length ());
System.out.println ("--------------------------");
The string literal "abc" can also be viewed as a string object.
String s7 = "ABCDE";
System.out.println ("S7:" +S7);
System.out.println ("S7.length ():" +s7.length ()); Output: S1:s1.length (): 0--------------------------S2:abcde s2.length (): 5--------------------------S3:BCD S3.lengtH (): 3--------------------------s4:abcde Ailin Pro s4.length (): 8--------------------------S5:cde Love S5.length (): 4---------
-----------------S6:ABCDE s6.length (): 5--------------------------S7:ABCDE s7.length (): 5
3 string features that cannot be changed once they are assigned
* * Character of the string: once assigned, it cannot be changed.
* * Public
class Stringdemo {public
static void Main (string[] args) {
String s = "Hello";
s + + "world";
System.out.println ("s:" + s); HelloWorld
}
}