Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Handling StringBuffer class character class StringTokenizer class.

Similar presentations


Presentation on theme: "String Handling StringBuffer class character class StringTokenizer class."— Presentation transcript:

1 String Handling StringBuffer class character class StringTokenizer class

2 StringBuffer class Provides much of the functionalities of Strings Represents growable and writable character sequences May have the charcters in the middle or appended to the end Will grow automatically Allow room for growth

3 StringBuffer Constructors StringBuffer() Creates with no parameters Reserves room for 16 characters without reallocation StringBuffer(int size) Explicitly sets the size of the buffer

4 Constructor StringBuffer(String str) Str sets the initial contents of the StringBuffer Reserves room for 16 more characters object without reallocation Reallocation is expensive process in terms of time Frequent reallocation can fragment memory

5 Methods length() Give number of characters or length of a StringBuffer int length() capacity() Total allocated capacity can be found int capacity()

6 ensureCapacity() capacity specifies the size of the buffer To preallocate room for a certain number of characters after a stringBuffer has been constructed To set the size of the buffer Useful if you know in advance that you will be appending a large number of small strings to a stringBuffer void ensureCapacity(int capacity)

7 setLength() To set the length of the buffer within a StringBuffer object If we increase the size of the buffer, null characters are added to the end of the existing buffer If we setLength() with a value less than the current value returned by length(), then the characters stored beyond the new length will be lost void setLength(int len)

8 charAt() Extraction of single character from a stringBuffer char charAt(int where) setCharAt() Set a specified character at the specified position void charAt(int where, char ch) getChars() To copy a substring of a StringBuffer into an array void getChars(int sourceStart, int sourceEnd, char target[], int targetStart)

9 append() To concatenate the string representation of any other type of data to the end of the invoking StringBuffer object StringBuffer append(String str) StringBuffer append(int num) StringBuffer append(Object obj) String.valueOf() is called for each parameter to obtain its string representation The result is appended to the current StringBuffer object

10 insert() Inserts one string into another It calls String.valueOf() to obtain the string representation of the value it is called with This string is then inserted into the invoking StringBuffer object StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj)

11 reverse() Can reverse the characters within a StringBuffer object Return the reversed object on which it is performed StringBuffer reverse() replace() Replaces one set of characters with another set inside a StringBuffer StringBuffer replace(int startIndex, int endIndex, String str)

12 delete() To delete characters from the StringBuffer object StringBuffer delete(int startIndex, int endIndex) Deletes the sequence of characters from the invoking object StringBuffer delete(int loc) Deletes a single character at that specified location Returns a StringBuffer object

13 substring() Returns the specified segment of the StringBuffer String substring(int startIndex) Returns the string from startIndex to the ned of the invoking StringBuffer object String substring(int startIndex, int endIndex) Returns the substring that starts at startIndex and runs thru end-1

14 Character class The type-wrapped class for character Most of the methods are static and take atleast a character argument Also contains a constructor that receives a char argument to initialize a character object Character(char ch) ch – specifies the character that will be wrapped by the Character object

15 char charValue() To obtain the char value contained in character object Returns the character static boolean isDefined(char ch) Returns true if ch is defined by unicode. static boolean isDigit(char ch) Returns true if ch is a digit.

16 static boolean isJavaIdentifierStart(char ch) Returns true if ch is allowed as the first character of the Java identifier static boolean isJavaIdentifierPart(char ch) Returns true if ch is allowed as the part character of the Java identifier(other than the first character) static boolean isLetter(char ch) Returns true if ch is a letter. static boolean isLetterOrDigit(char ch) Returns true if ch is a digit or a letter.

17 static boolean isLowerCase(char ch) Returns true if ch is a lower case letter static boolean isUpperCase(char ch) Returns true if ch is a upper case letter static boolean isSpaceCase(char ch) Returns true if ch is a space character static char toLowerCase(char ch) Returns lowercase equivalent of ch static char toUpperCase(char ch) Returns uppercase equivalent of ch

18 static char forDigit(int digit, int radix) Returns a char after converting the integer digit into a character in the number system specified by the integer radix(base of the number) static int digit(char ch, int radix) Returns the integer value after converting the character ch into an integer in the number system specified by the integer radix(base of the number) static boolean equals(Charater ch) Returns true if the contents in object ch is equals to contents in the invoked object

19 StringTokenizer class Is available in java.util package That breaks a string into its component tokens Tokens are seperated from one another using delimiters Default delimiters – blank, tab, new line, carriage return Other characters used as delimiters : ;,

20 Constructors StringTokenizer(String str) str is the string that will be tokenized the default delimiters are used StringTokenizer(String str, String delimiters) Delimiters is a string that specifies the delimiters StringTokenizer(String str, String delimiters, boolean delimAsToken) If delimAsToken true, then the delemiters are also returned as tokens when the string is parsed

21 Methods int countTokens() Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result String nextToken() Returns the next token as a string boolean hasMoreTokens() Returns true if one or tokens remain in the string and returns false if there is none


Download ppt "String Handling StringBuffer class character class StringTokenizer class."

Similar presentations


Ads by Google