Lecture 07 String Jaeki Song Outline String class
- Slides: 27
Lecture 07 String Jaeki Song
Outline • • • String class String comparisons String conversions String. Buffer class String. Tokenizer class
String • A string is a sequence of characters – Includes letters, digits, and various special characters, such as +, -, *, /, $ e. g. ) “Jaeki Song” “ISQS 6337”
Classes • In Java, a string is an object • Java provides three string classes – Java. lang package • string class – Storing and processing strings but strings created using the string class cannot be modified • string. Buffer class – Create flexible strings that can be modified – Java. util package • string. Tokenizer class – Can be used to extract tokens from a string
String Class • String class provide nine constructors and more than 40 methods for examining in individual characters in a sequence
String Constructor • You can create a string from a string value or from an array of characters String new. String = new String(String. Value); The argument String. Value is a sequence of characters enclosed inside double quotes e. g. ) String message = new String (“Welcome”); String message = “Welcome”;
Example: String. Constructor char. Array[]={'b', 'i', 'r', 't', 'h', 'd', 'a', 'y'}; byte. Array[]={(byte) 'n', (byte) 'e', (byte) 'w', (byte) 'y', (byte) 'e', (byte) 'a', (byte) 'r'}; String s, s 1, s 2, s 3, s 4, s 5, s 6, s 7, output; String s = new String ("hello"); String. Buffer buffer = new String. Buffer ("Welcome"); String String s 1 s 2 s 3 s 4 s 5 s 6 s 7 = = = = new new String( ); String(s); String(char. Array, 6, 3); String(byte. Array, 4, 4); String(byte. Array); String(buffer); output = "s 1 = " + s 1 + "ns 2 = " + s 2 + "ns 3 = " + s 3 + "ns 4 = " + s 4 + "ns 5 = " + s 5 + "ns 6 = " + s 6 + "ns 7 = " + s 7; JOption. Pane. show. Message. Dialog(null, output); System. exit (0);
length, char. At, and get. Chars Methods • Determine the length of string s 1. length( ) • Get the character at a specific location in a string s 1. char. At (1) • Get the entire set of characters in a string s 1. get. Chars (0, 5, char. Array, 0)
Example String output; String s 1 = new String( "hello there" ); char. Array[ ] = new char[ 5 ]; output = "s 1: " + s 1; output += "n. Length of s 1: " + s 1. length(); output += "n. The string reversed is: "; for ( int count = s 1. length() - 1; count >= 0; count-- ) output += s 1. char. At( count ) + " "; s 1. get. Chars( 0, 5, char. Array, 0 ); output += "n. The character array is: "; for ( int count = 0; count < char. Array. length; count++ ) output += char. Array[ count ]; JOption. Pane. show. Message. Dialog( null, output); System. exit( 0 );
String Comparisons • Java provides a various comparison methods – equals • Compare any two string objects for equality s 1. equals(“hello”); • Use lexicographical comparison – equals. Ignore. Case S 1. equals. Ignore. Case (s 2)
String Comparisons • compare. To s 1. compare. To(s 2) s 1 > s 2 positive number s 1 < s 2 negative number s 1 = s 2 zero e. g 1) s 1 “h” and s 2 “g” s 1. compare. To(s 2) 1 e. g 2) s 1 “abc” and s 2 “abe” s 1. compare. To(s 2) -2
String Comparisons • region. Matches compares portions of two String objects for equality s 1. region. Matches (0, s 2, 0, 5); s 1. region. Matches (true, 0, s 2, 0, 5); If the first argument is true, the method ignores The case of the characters being compared
Example • Compare. String
Extracting Substrings • substring method enable a new String object to be created by copying part of an existing String object substring (int start. Index) Copies the characters form the starting index to the end of the String substring(int begin. Index, int end. Index) Copies the characters from the starting index to one beyond the end. Index
Example String letters = “Happy Birthday” letters. substring(7) “irthday” letters. substring (0, 5) “Happy”
String Concatenation • Java provide the concat method to concatenate two strings String s 1 = new String (“Happy ”); String s 2 = new String (“Birthday”); s 1. concat(s 2); “Happy Birthday”
String Conversions • Generally, the contents of a string cannot be changed once the string is created • Java provides conversion methods – to. Upper. Case and to. Lower. Case • Return a new string by converting all the characters in the string to lowercase or uppercase – Trim • Returns a new string by eliminating blank characters from both ends of the string – Replace(old. Char, new. Char) • Can be used to replace a character in the string with a new character
Example • Example: Coverge. String
Converting Characters and Numeric Values to Strings • The String class provides value. Of methods for converting a character, an array of characters and numeric values to strings – value. Of method take different argument types
The String. Buffer Class • Alternative class • Can be used wherever a string is used – More flexible than String • Can add, insert, or append new contents into a string buffer • However, the value of string is fixed once the string is created • The String class has three constructors and more than 30 methods for managing the buffer and for modifying strings in the buffer – Every String. Buffer is capable of storing a number of characters specified by its capacity
String. Buffer Constructors public String. Buffer( ) – No characters in it and an initial capacity of 16 characters public String. Buffer (int length) – No characters in it and an inial capacity specified by the length argument public String. Buffer (String string) – Contains String argument and an initial capacity of the buffer is 16 plus the length of the argument
String. Buffer Methods • capacity method – Returns the current capacity of the string buffer • length method – Returns the number of characters in the string buffer • set. Length method – Sets the length of the string buffer • If the new. Length argument is less than the current length of the string buffer, then the string buffer is truncated to contain exactly the number of characters given by the new. Length argument • char. At method – Returns the character at a specific index in the string buffer • The first character of a string buffer is at index 0
String. Buffer Methods • You can append new contents at the end of a string buffer, insert new contents at a specified position in a string buffer, and delete of replace characters in a string buffer – Provides overload methods to append and insert boolean, char array, double, float, int, lng and String into string buffer
Example • Append and Insert String
The String. Tokenizer Class • Break a string into pieces (tokens) so that information contained in it can be retrieved and processed – How does the String. Tokenizer class recognize individual words? • Specify a set of characters as delimiters when constructing a String. Tokenizer object
The String. Tokenizer Class • Methods has. More. Token( ) • Returns true if there is a token left in the string next. Token( ) • Returns the next token in the string Next. Token(String delim) • Returns the next token in the string after reseting the delimiter to delim count. Token( ) • Returns the number of tokens remaining in the string tokenizer
Example import java. util. *; public class Test. Token { public static void main(String[] args) { String s = new String ("1000 Lucas Lubbock TX 79413"); String. Tokenizer tokens = new String. Tokenizer(s); System. out. println(tokens. count. Tokens()); while (tokens. has. More. Tokens()) { System. out. println(tokens. next. Token() + "n"); } } }
- Http protocol description
- Public class person
- Class maths student student1 class student string name
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Lecture outline example
- Lecture outline example
- Lecture outline example
- Lecture outline meaning
- Const char * vs char * const
- New string[1]
- Topic sentence sandwich
- What makes a song a song
- Giọng song song là gì?
- Hình chiếu vuông góc
- Cách chứng minh hai đường thẳng song song
- đoạn mạch mắc song song
- Cây vừa sống trên cạn vừa sống dưới nước
- Come let us sing a song a song declaring we belong to jesus
- Public class employee
- Class string
- Public class vehicle private string name
- Public class person private string name
- Public class animal private string name
- Class boo boo(string s) boo()
- Cstyle string
- Public class telephone { private string number
- Private string
- Public static void main(string[] args){