210 likes | 447 Views
String Handling. Fundamentals of Characters and String String class - Constructors String Methods Operations on Strings Character Extraction String Comparision Searching Strings Modifying a String Data Conversion Changing the case of Characters. Fundamentals of Characters and Strings.
E N D
String Handling • Fundamentals of Characters and String • String class - Constructors • String Methods • Operations on Strings • Character Extraction • String Comparision • Searching Strings • Modifying a String • Data Conversion • Changing the case of Characters
Fundamentals of Characters and Strings • Characters – the building blocks of source code • String – is a series of characters treated as a single unit • Creating a string object => creating a string that cannot be changed • A new object is created that contains the modifications • Original string is left unchanged
Constructors • String() • Will create an instance of String with no characters in it • String(char chars[]) • Will create a string initialized by an array of characters • String(char chars[], int startIndex, int num) • Can specify a subrange of a character array as an initializer
Constructors • String(String strObj) • Can construct a String object that contains the same character sequence as another String • String(byte asciiChars[]) • Can initialize a string of a byte array is given • String(byte asciiChars[], int startIndex, int numChars) • Can specify a subrange of a byte array as an initializer
Methods • Length() • the number of characters that the string contains • int length() • If String s; S.length()
Special String Operations • String Literals • Is a string constant • StringLiteral.length() • String Concatenation • Use + operator • Concatenates string objects/strings and produces string object as a result • s = s1 + s2 • Concatenation with other data types
toString() • String toString() • Is a method of an Object • Each and every class will have this method • To provide our own string representation we will override the toString() • Returns a string object • Describes an object of the class
Character Extraction • charAt() • To extract a single character of the string • char charAt(int where) • getChars() • To extract more than one character from a string • Void getChars(int sourceStart, int sourceEnd, char target[], int targetStart)
getBytes() • Stores the characters in an array of bytes • Uses the default character-tobyte conversions provided by the platform • byte [] getBytes() • toCharArray() • To convert all the characters in a string object into a character array • Char[] toCharArray()
String Comparision • equals() • To compare two strings for equality • boolean equals(Object str) • equalsIgnoreCase() • Comparision that ignores case differences • boolean equalsIgnoreCase(Object str)
regionMatches() • Compare a specific region inside a string with another specific region in another string object • boolean regionMatches(int startIndex, String str2 , int str2StartIndex, int numChars) • Comparision that ignores case differences • boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)
startsWith() and endsWith() • startsWith – determines whether a string begins with a specified string • boolean startsWith(String str) • endsWith() – determines whether a string ends with a specified string • boolean endsWith(String str) • boolean startsWith(String str, int startIndex) • Lets you specify a starting point
compareTo() • Compares the string is less than / equal to / greater than any another string. • int compareTo(String str) • int compareToIgnoreCase(String str) • ValuesMeaning <0 the invoking string less than str >0 the invoking string greater than str =0 the two strings are equal
Searching Strings • To search a string for a specified character or substring • Returns the index at which the character or substring was found • int indexOf(int ch) • Searches for the first occurrence of a character • int lastIndexOf(int ch) • Searches for the last occurrence of a character
To search the first/last occurrence of a substring • int indexOf(String str) • int lastIndexOf(String str) • To specify the starting point for the search • int indexOf(int ch, int startIndex) • int lastIndexOf(int ch, int startIndex) • int indexOf(String str, int startIndex) • int lastIndexOf(String str, int startIndex)
Modifying a String • subString() • can extract a sub string • String subString(int startIndex) • String subString(int startIndex, int endIndex) • concat() • Can concatenate two strings • Creates a new object that contains the invoking string with the contents of str appended to the end • String concat(String str)
replace() • Replaces all occurences of one character in the invoking string with another character • String replace(char original, char replacement) • trim() • Return a copy of the invoking string from which any leading and trailing white space has been removed • String trim()
Data Conversion • valueOf() • Converts data from its internal format into a human-readable form • Is a static method overloaded within string for all of java’s built in types, so that each can be converted properly to string • Is called when a string representation of some other datatype is needed
Static String valueOf(double num) • Static String valueOf(long num) • Static String valueOf(Object obj) • Static String valueOf(char chars[]) • To specify a subset of char array use • Static String valueOf(char chars[], int startIndex, int numChars)
Changing the Case • toLowerCase() • Converts all the characters in a string from uppercase to lowercase • String toLowerCase() • toUpperCase() • Converts all the characters in a string from lowercase to uppercase • String toUpperCase()