string handling stringbuffer class character class stringtokenizer class

21
String Handling StringBuffer class character class StringTokenizer class

Upload: emmeline-ashlie-thornton

Post on 14-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: String Handling StringBuffer class character class StringTokenizer class

String Handling

StringBuffer class character class StringTokenizer class

Page 2: String Handling StringBuffer class character class StringTokenizer class

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

Page 3: String Handling StringBuffer class character class StringTokenizer class

StringBuffer Constructors StringBuffer()

Creates with no parameters Reserves room for 16 characters

without reallocation StringBuffer(int size)

Explicitly sets the size of the buffer

Page 4: String Handling StringBuffer class character class StringTokenizer class

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

Page 5: String Handling StringBuffer class character class StringTokenizer class

Methods length()

Give number of characters or length of a StringBuffer

int length() capacity()

Total allocated capacity can be found int capacity()

Page 6: String Handling StringBuffer class character class StringTokenizer class

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)

Page 7: String Handling StringBuffer class character class StringTokenizer class

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)

Page 8: String Handling StringBuffer class character class StringTokenizer class

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)

Page 9: String Handling StringBuffer class character class StringTokenizer class

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

Page 10: String Handling StringBuffer class character class StringTokenizer class

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)

Page 11: String Handling StringBuffer class character class StringTokenizer class

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)

Page 12: String Handling StringBuffer class character class StringTokenizer class

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

Page 13: String Handling StringBuffer class character class StringTokenizer class

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

Page 14: String Handling StringBuffer class character class StringTokenizer class

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

Page 15: String Handling StringBuffer class character class StringTokenizer class

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.

Page 16: String Handling StringBuffer class character class StringTokenizer class

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.

Page 17: String Handling StringBuffer class character class StringTokenizer class

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

Page 18: String Handling StringBuffer class character class StringTokenizer class

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

Page 19: String Handling StringBuffer class character class StringTokenizer class

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

: ; ,

Page 20: String Handling StringBuffer class character class StringTokenizer class

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

Page 21: String Handling StringBuffer class character class StringTokenizer class

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