130 likes | 305 Views
METHODS FOR CHARACTER EXTRACTION,STRING SEARCHING AND COMPARISON. PRESENTED BY : DIPU KALITA DC2012MCA0022. INTRODUCTION.
E N D
METHODS FOR CHARACTER EXTRACTION,STRING SEARCHING AND COMPARISON PRESENTED BY : DIPU KALITA DC2012MCA0022
INTRODUCTION CHARACTER EXTRACTION: The string class provides a number of ways in which characters can be extracted from a string object. Several are examined here. Although the characters that comprise a string within a string object cannot be indexed as if they were a character array, many of the string methods employ an index (or offset) into the string for their operation. Like arrays, the string indexes begin at zero.
String searching: • Many software applications use the basic string search algorithm in the implementations on most operating systems. • The popularity of Internet, the quantity of available data from different parts of the world has increased dramatically within a short time. • A string search algorithm that is language-aware has become more important. • A bitwise match that uses the u_strstr (C), UnicodeString::indexOf (C++) or String.indexOf(Java).
STRING COMPARISON: String comparison can be done in 3 ways: 1.Using equals() method: equals() method compares two strings for equality. It compares the content of the strings. 2.Using ==operator method: ==operator compares two object references to check whether they refer to same instance. 3. By compare to() method: compare to() methods compares values returns an into which tells if the string compared is less than, equal to or greater than the other string. To use this function you must implement the comparable interface. Compare To() is the only function in comparable interface.
DESCRIPTION CHARACTER EXTRACTION: METHOD DESCRIPTION Java - String charAt() This method returns the character located at the String's specified index. The string indexes start from zero. STRING SEARCHING: METHOD DESCRIPTION Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring,-1 is returned. intindexOf(String str):
STRING COMPARISON : METHOD DESCRIPTION There are two variants of this method. First method compares this String to another Object and second method compares two strings lexicographically. Java - String compareTo()
Sample PROGRAME CHARACTEREXTRACTION: OUTPUT:Gurukul
STRING SEARCHING: public class SearchStringEmp{ public static void main(String[] args) { String strOrig = "Hello readers"; intintIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index “ + intIndex); } } } OUTPUT: Found Hello at index is 0
STRING COMPARISON: public class StringCompareEmp{ public static void main(String args[]){ String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString)); System.out.println(str.compareToIgnoreCase(anoting) ); System.out.println( str.compareTo(objStr.toString())); } } OUTPUT: -32 0 0