1 / 20

String Handling

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.

les
Download Presentation

String Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. Methods • Length() • the number of characters that the string contains • int length() • If String s; S.length()

  6. 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

  7. 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

  8. 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)

  9. 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()

  10. String Comparision • equals() • To compare two strings for equality • boolean equals(Object str) • equalsIgnoreCase() • Comparision that ignores case differences • boolean equalsIgnoreCase(Object str)

  11. 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)

  12. 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

  13. 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

  14. 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

  15. 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)

  16. 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)

  17. 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()

  18. 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

  19. 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)

  20. 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()

More Related