Other features in the String class:
Replacement function:
* Public string replace (char Oldchar,char Newchar): Replaces one character in a string with a new character
* public string Replace (string oldstr,string newstr): Replaces a substring in a string with a new string
* public string trim (): Remove whitespace at both ends of string:
* public int compareTo (String anotherstring) is a method in the comparable interface (the interface can implement a natural sort)
* Comparator interface can be sorted by comparator
Package String;public class StringDemo1 {public static void main (string[] args) {//define string str= "Helloword"; System.out.println (str);//Replace the character "L" in the original string with "K", String Str1=str.replace ("L", "K"); System.out.println (STR1);//The string "Hello" in the original string is replaced by "hi--" with a string str2=str.replace ("Hello", "hi--"); System.out.println (STR2); System.out.println ("----------------"); String str3= "Helloword"; System.out.println (STR3);//Returns a copy of the string, ignoring leading whitespace and trailing blank string Str4=str3.trim (); System.out.println (STR4); System.out.println ("----------------"); String STR5 = "Helloword"; String str6= "ABC"; System.out.println ("CompareTo ():" +str5.compareto (str)); System.out.println ("CompareTo ():" +str6.compareto (str));}}
650) this.width=650; "title=" String2.png "alt=" 27f1921f89f20f251f23ddcb185610fa.png-wh_ "src=" https://s5.51cto.com /oss/201711/02/27f1921f89f20f251f23ddcb185610fa.png-wh_500x0-wm_3-wmp_4-s_3341893237.png "/>
* int indexOf (string str): Returns the index of one occurrence of the specified string in this string
* int indexOf (stirng str,int FromIndex): Returns the index of the first occurrence of the specified string in the string, starting at the specified index
* String substring (int start): Truncate string starting at the specified position, default to the end
* String substring (int start,int end): Intercept from the specified position to the end of the specified position
Package String;import Java.util.scanner;public class StringDemo5 {public static void main (string[] args) {string str= " Java "; Scanner sc=new Scanner (system.in); System.out.println ("Please enter a string:"); String Str1=sc.nextline (); System.out.println ("The string you entered is:" +STR1); System.out.println ("----------------");//Return to the first address in Java from the beginning System.out.println ("Str1.indexof (str):" + Str1.indexof (str));//ugfuyjavaojgojjavagfdug//starts at the specified index System.out.println ("Str1.indexof (str,7):" +str1.indexof ( str,7)); System.out.println ("-------------------");//intercept the string from the specified position, by default to end System.out.println ("Str1.substring (3):" + Str1.substring (3));//intercept the string from the specified position, end System.out.println at the specified position ("Str1.substring (3,7):" +str1.substring (3,7));}}
650) this.width=650; "title=" String3.png "alt=" 6314e9f6eee39f572aea1e840cb4c2a9.png-wh_ "src=" https://s4.51cto.com /oss/201711/02/6314e9f6eee39f572aea1e840cb4c2a9.png-wh_500x0-wm_3-wmp_4-s_289388021.png "/>
* StringBuffer class
Thread-Safe, variable-character sequence, string-like buffer that resembles string
* If the development of the thread is not safe, there may be a deadlock phenomenon! Thread safety and execution efficiency are relative
* face test
What is the difference between *stringbuffer and string?
*stringbuffer constructs a string buffer, considering the use of StringBuffer from a memory perspective
String is an ordinary string class that consumes space from the memory angle!
Use Stringbulider (thread insecure) in a single-threaded program instead of StringBuffer
the common methods in *stringbuffer
* Construction Method:
*public StringBuffer () { }
Constructs a string buffer with no characters, the initial capacity of the string buffer is 16 characters
*public stringbuffer (int capacity)
Constructs a string buffer with no characters, but with the specified initial capacity
*public StringBuffer (String str)
Constructs a string buffer and initializes its contents to the specified string content. The initial capacity of the string is 16 plus the length of the string parameter.
* public int length (); Get string length
* public int capacity (): Gets the capacity of the current string buffer
*How to add StringBuffer:
* Public stringbuffer append (); Append data to buffer, return string buffer itself
* Public stringbuffer Insert (int offset,string str): Adds a string at the specified location, returning the string buffer itself
*stringbuffer Method of deletion:
* Public stringbuffer Deletecharat (int start): Deletes the character at the specified position, returns the string buffer itself
* Public stringbuffer Deletecharat (int start,int end): Removes the character returned from the specified position to the end of the specified position Back to the string buffer itself
Package Stringbuffer;public class Stringbufferdemo {public static void main (string[] args) {// StringBuffer can not be assigned directly as a string, you can create an object using the constructor method assignment//can also call append () through the non-parametric construction, method to add StringBuffer sb=new stringbuffer (); Sb.append ("Hello"), Sb.append ("World"), Sb.append ("-----javase"); SYSTEM.OUT.PRINTLN ("Add initial string:" +SB); Sb.insert (5, "-----"); System.out.println ("Add with Insert ():" +SB); System.out.println ("----------------"); Sb.deletecharat (3); System.out.println ("Delete a character after the string:" +SB); System.out.println ("----------------");//The character that corresponds to the current index has changed Sb.delete (1, 3); System.out.println ("After deleting a small string:" +SB); System.out.println ("----------------");}}
650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/03/9672ad13b84d5b0fa0cbcff2f789b9ec.png-wh_500x0-wm_3 -wmp_4-s_3940078985.png "title=" Append.png "alt=" 9672ad13b84d5b0fa0cbcff2f789b9ec.png-wh_ "/>
Conversion between *stringbuffer and string
*stringbuffer reversal Function:
Public StringBuffer reserve (); Flips the string order directly
package stringbuffer;public class stringbufferdemo1 {public static void Main (String[] args) { string str= "Hello"; // String-----Conversion of >stringbuffer system.out.println ("string----->stringbuffer conversion"); //the object that created StringBuffer points to this string StringBuffer sb=new StringBuffer (str); system.out.println ("The object that created the StringBuffer points to this string:" +SB); //uses the parameterless construct to invoke the Append () method stringbuffer sb1=new stringbuffer (); sb1.append (str);     SYSTEM.OUT.PRINTLN (" Using the parameterless construct, call the Append () method: "+SB1); system.out.println ("----------------"); //stringbuffer----->string Conversion system.out.println (" StringBuffer-----Conversion of >string "); //Create buffer Object stringbuffer sb2=new stringbuffer ("World"); //string has a construction method String (STRINGBUFFER STR) String sb3=new String (SB2); system.out.println ("Construction method String (STRINGBUFFER STR):" +SB3); //tostring () method string sb4=sb2.tostring (); System.out.println ("ToString () Method:" +SB4); system.out.println ("----------------"); //use StringBuffer's flip function to invert the string "reverse ()" System.out.println ("String type" Hello "after using reverse () is flipped:" +sb2.reverse (). toString ()); }}
650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/03/bedb92d12cf1648edb42887697b54fbd.png-wh_500x0-wm_3 -wmp_4-s_286552101.png "title=" Reverse.png "alt=" Bedb92d12cf1648edb42887697b54fbd.png-wh_ "/>
*stringbufferd Replacement function:
*public stringbuffer replace (int start,int end,string str): From the specified position to the specified position with STR Change
*stringbuffer interception function:
*public stringbuffer substring (int start): Returns a new string from the beginning to the end of the specified position
*public stringbuffer substring (int start,int end): Returns a new string, starting at the specified position and ending at the specified position
Package Stringbuffer;public class StringBufferDemo2 {public static void main (string[] args) {StringBuffer sb=new StringBuffer (); Sb.append ("HelloWorld"); System.out.println ("Initial string:" +SB); System.out.println ("Replaced:" +sb.replace (0, 5, "hi-")); System.out.println ("------------------");//stringbuffer interception function//from the specified position, the default to the end of the end of the System.out.println ("intercept from the specified position: "+sb.substring (5)); System.out.println ("End from specified position to specified position:" +sb.substring (3, 6));}}
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/03/eca29d24d9bad03094077e21203f77a6.png-wh_500x0-wm_3 -wmp_4-s_1028603263.png "title=" Replace.png "alt=" Eca29d24d9bad03094077e21203f77a6.png-wh_ "/>
Questions:
The difference between *string, StringBuffer and Stringbuleder
String: An immutable sequence of characters
StringBuffer and Stringbuleder are variable character sequences that take precedence over a single thread Stirngbuleder
* From a threading perspective: Stringbuleder threads are unsafe, out of sync, performing efficiently, faster than StringBuffer
*string and StringBuffer are the same as formal parameters and basic types
*integer class
New features after jdk5.0: Automatic disassembly box, variable parameters ...
Each base data type is encapsulated as a reference type
Base type Reference type
int Integer
Char Character
BYTE byte
Long Long
Double Double
Short Short
float float
Boolean Blooean
How the Integer class is constructed:
public Integer (int value): Encapsulates data of an int type into a reference type
Public Integer (String s): Encapsulates a character number type as an Integer type
The string must be a numeric string or run an error: Java.lang.NumberFormatException
Package Integer;public class Integerdemo {public static void main (string[] args) {//public static String tobinarystring (in t i)//returns the string representation of an integer parameter as a binary unsigned number System.out.println (integer.tobinarystring ());//public static string tooctalstring (int i)//returns a string representation of an integer argument as an octal unsigned number System.out.println (integer.tooctalstring);//public static string tohexstring (int i)//returns the strings representation of an integer parameter in the form of a hexadecimal unsigned number System.out.println (integer.tohexstring);//public The maximum value that the static final int max_value//int type can represent is//public the minimum value that the static final int min_value//int type can represent SYSTEM.OUT.PRINTLN ( Integer.max_value);//2147483647system.out.println (Integer.min_value);//-2147483647}}
650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/03/2e67c909b5b362e214f27c524e6b547f.png-wh_500x0-wm_3 -wmp_4-s_3133664895.png "title=" Integer.png "alt=" 2e67c909b5b362e214f27c524e6b547f.png-wh_ "/>
*int types and types of string conversions
*public int intvalue ();//Returns the value of the integer as an int type
*public static int parseint (string str);//parse the string argument as a signed number decimal integer
*character class
* Wrap the value of a base type char in an object containing a field of type char for an object of type character
* Construction Method:
Public Character (char value)://Constructs a newly assigned Character object to represent the value of the developed Char
*character's judgment function and conversion function
public static Boolean islowercase (char ch) determines whether the specified character is a lowercase letter.
*public Static Boolenn isuppercase (char ch) determines whether the specified character is uppercase
*public Static Boolean isdigit (char ch) determines whether the specified character is a number.
*public Static char touppercase (char ch): Converts the specified character to uppercase
*public Static char tolowercase (char ch): Converts the specified character to lowercase
Package character;public class characterdemo { public static void main (String[] args) {//public static boolean islowercase (char ch Determines whether the specified character is a lowercase letter System.out.println ("Islowercase:" +character.islowercase (' a ')); System.out.println ("Islowercase:" +character.islowercase (' A ')); System.out.println ("Islowercase:" +character.islowercase (' 0 ')); System.out.println ("---------------------------------------");//public static boolenn Isuppercase (CHAR CH) determines whether the specified character is a capital letter System.out.println ("Isuppercase:" +character.isuppercase (' a ')); System.out.println ("Isuppercase:" +character.isuppercase (' A ')); System.out.println ("Isuppercase:" +character.isuppercase (' 0 ')); System.out.println ("---------------------------------------");//public static boolean isdigit ( CHAR CH) Determines whether the specified character is a number. System.out.println ("IsDigit:" +character.isdigit (' a ')); System.out.println ("IsDigit:" +character.isdigit (' A ')); System.out.prinTLN ("IsDigit:" +character.isdigit (' 0 '));}}
650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/03/fbfacc6218a3c0f745455934b95007e7.png-wh_500x0-wm_3 -wmp_4-s_1607661670.png "title=" Character.png "alt=" Fbfacc6218a3c0f745455934b95007e7.png-wh_ "/>
Java Learning Lesson 11