Strings And other things Strings Overview The String
- Slides: 22
Strings And other things
Strings Overview The String Class and its methods n The char data type and Character class n String. Builder, String. Tokenizer classes n Printf() method n The String. Buffer class n
What is a string A sequence of characters n Representing alphanumeric data. n Defined in the java. lang. String class n String is a class and objects of String type are reference variables. n
Making a string Declared as a typical object would be. n String s= new String(“Hello”) n Or using a short hand that java provides n String s = “Hello” n
The string class provides many methods. n n n char. At(int idx) compare. To(string s) concat(string s) equals(string s) index. Of(int ch, int fromhere) last. Index. Of(int ch, int fromhere) length() replace(char old, char new) substring(int beginidx, int endidx) trim() to. Upper(String s)
Joining strings Concatenation is the operation of joining one string on the end of another. n S 3 = S. concat(S 2) n Or S 3 = S + S 2 n Java will implicitly type cast a numeric value as a string n
String literals “Hello” n “Good bye” n “ 10, 100” n “ 576 DSW” n “ 700 -6000” n “Bill” n
String equality n String s 1=“Hello” String s 2=“Hello” n If (s 1==s 2) System. out. print(“Equal”) n ¨ n If (s 1. equals(s 2)) System. out. print(“Equal”) ¨ n n Not equal Equal String s 3=s 1 If (s 3==s 1) System. out. print(“Equal”) ¨ Equal
String comparisons If S 1 < S 2 may not work correctly n If (S 1. compare. To(s 2)) < 0 System. out. print(“Less then”) n If (S 1. compare. To(s 2)) == 0 System. out. print(“Equals”) n If (S 1. compare. To(s 2)) > 0 System. out. print(“Greater then”) n
Finding strings index. Of(int ch, int fromhere) n last. Index. Of(int ch, int fromhere) n index. Of(String S, int fromhere) n S 1=“Hello” n S 1. indexof(‘l’) n ¨ Will n return 2 S 1. lastindexof(‘l’) ¨ Will return 3
String Conversions n String. valueof(number) ¨ Returns n a string And Integer. parseint(String) ¨ Return a number
Converting Strings To. Lower. Case() n To. Uppercase() n
The stringbuffer class Like the string class n Why 2 string classes? n Other methods: n. delete(int loc, int toloc) n. reverse() n. capacity() the size of the stringbuffer n
Char data type n n n A char is a single unicode value Unicode is a code used to specify character data. char c; char[] carray = { ‘H’, ‘e’, ‘l’, ‘o’ }; A character array can be used to build a string String s= new String(carray)
Command line arguments are passed to the args array in the main method. n We can find the length of this array and process these arguments in our program. n
That Character class n A wrapper class ¨ These are likes like Double and Integer which can be used in place of the primitive data types. ¨ There is one for each primitive data type n Provides methods like: ¨ compare. To( char) ¨ equals(char) ¨ Character. is. Digit(char) // a static method ¨ Also: is. Letter(char) and is. Lower. Case()…
String. Builder n n n n An object that can be created to manipulate strings values mutably then store the result back into a string. Methods: append( String) insert(index, String) delete. Char. AT( index) replace( startindex, endindex, string) to. String()
String. Tokenizer n n Used to break strings apart. This process is also known as parsing a string. A token is a special character used to delimit the parts of a string. next. Token() ¨ count. Tokens() ¨ n n The default token is set to “ tnrf” the white space characters. We can change this using the optional constructor. . ¨ String. Tokenizer( string, token. String)
Formatting data n n The printf() method of Print. Stream class Format specifiers: ¨ %. 2 f ¨ %d ¨ %x n n - a double value, 2 decimals - an integer - hexvalue System. out. printf(“data = %. 2 f”, data); Formatting dates: ¨ %t. A, %t. B, %td, %t. Y ¨ For day, month, dayof month, year
Argument index value n n n System. out. printf(“%1$d, %2$. 2 f” , int, double) The 1$ and 2$ are used to specify the index of the parameter to use. By default the value of 1 is used. Field width: %3 d - specifies the integer with room for 3 digits
Justification n %-15 s %15 s n The format method n - left justify a string of 15 characters - default right justify the string ¨ Formatter f = new Formatter() ¨ F. format( “formatstring”, args…) n Or ¨ outstring =String. format( formatstring, args…)
Summary Strings n Characters n Char n Format and sprint methods n Importance of API documentation n
- Sprint tokenizer
- Http protocol description
- Things not strings
- Const char *s
- Public class person private string name
- Str-1
- Ecosystem living and nonliving things
- Other initiated self repair example
- Things that attract each other
- Supply and demand board game
- Springs and strings
- Pointers and strings
- Seven life processes of living things
- Hình ảnh bộ gõ cơ thể búng tay
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Voi kéo gỗ như thế nào
- Chụp tư thế worms-breton
- Chúa yêu trần thế
- Môn thể thao bắt đầu bằng từ đua
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới