String class, StringBuffer class, MATH class

xiaoxiao2021-03-06  89

This article will introduce some of Java's major classes, String classes, StringBuffer classes, MATH classes. Java is a real object-oriented language, even if it is a simple program, it must also design an object. Java itself also provides us with a number of well-designed classes. To be flexible to use Java, familiar with Java's major classes will be one of the essential prerequisites. String Category, String is the meaning of strings, this class is a class of string constants. I believe how people who use C-language programming know what is going on, here is no longer described. But one thing to explain is that the strings in the string and C language in Java are different. In C language, there is no true string, and the string in the C language is the character array, which is very flexible. In Java, string constants is a class ?? String class, which is different from the character array. Let's first introduce the constructor of the String class Public String () This constructor is used to create an empty string. Such as: string test = new string (); or: string test; test = new string (); public string (String value) This constructor creates a new string constant with an existing string constant as a parameter. It is also worth noting that Java creates an object of a String class for each string constant enclosed in double quotes "...". Such as: String K = "Hi."; Java will create a String class object for "Hi.", Then assign this object to K. I am equivalent: string temp = new string ("hi."); String K = Temp; this constructor usage is: string test = new string (k); (Note: k is an object of a String class) String test = New String ("Hello, World."); Public String (Char value []) This constructor uses a character array as a parameter to create a new string constant. Usage such as: char z [] = {'h', 'e', ​​'l', 'L', 'o'}; string test = new string (z); (Note: At this time, content is " Hello ") Public String (CHAR VALUE [], int off, int count) This constructor is an expansion of the previous, in a sentence, is the use of the character array value, from the origin of the Offset characters to COUNT characters Create an object of a String class. Usage such as: char z [] = {'h', 'e', ​​'l', 'l', 'o'}; string test = new string (z, 1, 3); (Note: Test at this time) The content is "ELL") Note: Array, subscript 0 represents the first element, 1 represents the second element ... If the start point OFFSET or the number of COUNT off-line will produce an exception stringIndexoutofboundsexceptionPublic String (StringBuffer buffer) The constructor uses a StringBuffer object as a parameter as a parameter to create a new string constant.

The String class is a string constant, and the StringBuffer class is a string variable, which is different. The StringBuffer class will be introduced later. The method of the String class is: public char charat (int index) This method is used to get one character in the string constant. Parameters index specifies the return from the string, which returns a character variable. Usage, string s = "hello"; char k = s.charat (0); (Note: At this time, the value of K ') PUBLIC INT COMPARETO (String Anotherstring) This method is used to compare the size of the string constant The parameter AnotherString is another string constant. If the two string constants are the same, the return value is 0. If the current string constant is large, the return value is greater than 0. If the other string constant is large, the return value is less than 0. Usage: string S1 = "abc"; string S2 = "abd"; int result = s2.compareto (S1); (Note: The value of Result is greater than 0, because D is ranked behind C in the ASCII code, then S2 > S1) Public String Concat (String Str) This method will put the parameters ?? String constance STR followed by the current string constant, generate a new string constant, and return. Usage, string s1 = "how"; string s2 = "you do?"; String ss = s1.concat (S2); (Note: The value of SS is "How do you do?") Public Boolean StartSwith (String Prefix) This method determines that the current string constant starts with parameters. ??? prefix string. Yes, return TRUE. No, return false. Usage: string s1 = "abcdefg"; string s2 = "bc"; boolean results = s1.startswith (s2); (Note: Result value false) Public Boolean StartSwith (String Prefix, int Toffset) This overload method The new parameter Toffset specifies the starting point of the lookup. Public Boolean Endswith (String Suffix) This method determines whether the current string constant ends with parameters ?? SUFFIX string constant. Yes, return TRUE. No, return false. Usage, string s1 = "abcdefg"; string s2 = "fg"; boolean results = s1.endswith (S2); (Note: Result is true) Public void getchars (int srcbegin, int srcend, char dst [] , Int dstbegin This method is used to intercept a string from a string constant and convert to a character array. The parameter srcbegin is the starting point of the interception, Srcend is the end point of the interception, DST is the target character array, and DstBegin specifies what is placed in the character array of strings that will be intercepted.

In fact, srcend is an end point of the interception 1, Srcend-srcbegin is the number of characters to be intercepted, such as: string s = "abcdefg"; char z [] = new char [10]; s.getChars (2, 4 , z, 0); (Note: Z [0] value of 'c', z [1] is 'd', intercepting two characters 4-2 = 2) Public int indexof (int CH) this The return value of the method is the position where the character ch is first appearing from left to right in the string constant. Returns -1 if there is no character in the string constant. Usage, such as: string s = "abcdefg"; int R1 = S.indexOf ('c'); int R2 = S.indexOf ('x'); (Note: R1 is 2, R2 value is -1) Public int indexof (int CH, int.comIndex) This method is the startset of the last method of overloading, the new parameter fromNDEX is the starting point of the lookup. Usage such as: string s = "abcdaefg"; int R = s.indexof ('a', 1); (Note: R) PUBLIC INDEXOF (String Str) This overload method returns a string constant STR The position of the left to right will appear from left to right in the current string constant. If the current string constant does not contain a string constant STR, -1 is returned. Usage such as: string s = "abcdefg"; int R1 = s.indexof ("CD"); int R2 = S.indexOf ("CA"); (Note: R1 is 2, R2 value is -1) Public int indexof (String str, int.comIndex) This overload method adds a new parameter fromNDEX to find the starting point of the lookup. The following four methods are similar to the four methods of the above, just find the right left in the string constant. public int lastIndexOf (int ch) public int lastIndexOf (int ch, int fromIndex) public int lastIndexOf (String str) public int lastIndexOf (String str, int fromIndex) public int length () This method returns a string constant length, which is One of the most common methods. Usage such as: string s = "abc"; int result = s.Length (); (Note: Result is 3) Public char [] TOCHARARRAY () This method converts the current string constant into a character array and returns.

Usage such as: string s = "who area you?"; Char z [] = S.ToChararray (); public static string valueof (Boolean b) public static string valueof (CHAR C) Public Static String Valueof (INT i) public static String Valueof (long L) Public Static String Valueof (Float F) Public Static String Valueof (Double D) The above 6 methods can convert Boolean, CHAR, INT, long, float, and double type variables to String class objects . Usage, such as: string r1 = string.valueof (True); (Note: R1 is "true") String R2 = String.Valueof ('c'); (Note: The value of R2 is "c") float ff = 3.1415926; String R3 = String.Valueof (ff); (Note: R3 value "3.1415926") The StringBuffer class String class is a string constant, which is an uncharged constant. StringBuffer is a string variable, and its object can be expanded and modified. The constructor of the String class PUBLIC STRINGBUFFER () Creates an object of an empty StringBuffer class. Public StringBuffer (INT Length) Creates a length of the StringBuffer class with a length of the parameter Length. Note: If the parameter Length is less than 0, the NegativeArraysizeException will be triggered. Public StringBuffer (String Str) Creates an object of the StringBuffer class with an existing string constant. The StringBuffer class is: public string toString () Convert to String class objects and return. Since most classes are more parameters regarding the displayed methods, the StringBuffer class objects are often converted to the String class object, and then it is displayed. Usage such as StringBuffer SB = New StringBuffer ("How are you?"); Label l1 = new label (Note: Declaring a label object L1, L1 content is how are you?) Public (double d) above six methods StringBuffer append (boolean b) public StringBuffer append (char c) public StringBuffer append (int i) public StringBuffer append (long l) public StringBuffer append (float f) public StringBuffer append may be boolean, char , Int, long, float, and double types of variables are appended to the back of the StringBuffer class.

Usage such as: double d = 123.4567; StringBuffer SB = new stringbuffer (); sb.append (true); sb.append ('c'). Append (d) .append (99); (Note: The value of SB is Truec123 .456799) Public StringBuffer Append (String STR) Adds the string constance STR to the back of the StringBuffer class. Public StringBuffer Append (Char Str []) Adds the character array STR to the back of the object of the StringBuffer class. Public StringBuffer Append (Char str [], int offset, int LEN) The character array STR, starts the LEN character from the origin of the OFFSET, and append the object of the StringBuffer class. public StringBuffer insert (int offset, boolean b) public StringBuffer insert (int offset, char c) public StringBuffer insert (int offset, int i) public StringBuffer insert (int offset, long l) public StringBuffer insert (int offset, float f) public StringBuffer insert (int offset, double d) public StringBuffer insert (int offset, String str) public StringBuffer insert (int offset, char str []) the boolean, char, int, long, float, double type variable, String class The object or character array is inserted into the origin of the object of the StringBuffer class. Usage such as: StringBuffer SB = New StringBuffer ("abfg"); sb.insert (2, "CDE"); (Note: The value of SB is abcdefg) public int length () This method returns the length of the string variable, usage and The length method of the String class is similar. The MATH mathematics class contains many mathematical functions, such as SiN, COS, Exp, ABS, etc. The MATH class is a tool class that has a very important role in resolving some issues related to mathematics. This class has two static properties: E and PI. E represents E 2.7182818 in mathematics, and PI represents PI 3.1415926. When referenced, the methods such as Math.e and Math.pi include: Public Static Int ABS (INT A) Public Static Flong ABS (Float a) Public Static Double ABS (Double A) PUBLIC STATIC DOUBLE ABS (Double A The ABS method is used to seek absolute values. Public Static Native Double ACOS (Double A) ACOS seeks anti-cosine functions. Public Static Native Double Asin (double a) asin seeks an anti-sinus string function. Public Static Native Double Atan (Double A) Atan asks for an anti-fixed function. Public Static Native Double CEIL (Double A) CEIL returns the smallest integer greater than A. Public Static Native Double Cos (Double A) COS Scenate Function.

转载请注明原文地址:https://www.9cbs.com/read-107998.html

New Post(0)