charAt( ) - SNS Courseware

Download Report

Transcript charAt( ) - SNS Courseware

Character Extraction
• charAt() - charAt() function returns the character
located at the specified index.
String str = “sunset";
System.out.println(str.charAt(2)); Output: n
• getChars() - extract more than one character at a time
void getChars(intsourceStart, intsourceEnd, chartarget[ ], inttargetStart)
• getBytes( ) - alternative togetChars( )that stores the
characters in an array of bytes.
byte[ ] getBytes( )
• toCharArray( ) - convert all the characters in a String
object into a character array
char[ ] toCharArray( )
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
Output :
demo
String Comparison
• equals() and equalsIgnoreCase()
boolean equals (Object str)
• Eg
String s = "Hell";
String s1 = "Hello";
String s2 = "Hello";
s1.equals(s2); //true
s.equals(s1) ; //false
equalsIgnoreCase() determines the equality of two Strings, ignoring
thier case
boolean equalsIgnoreCase(String str)
Eg.
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
• regionMatches() - compares a specific region
inside a string with another specific region in
another string.
• boolean regionMatches(int startIndex, String
str2,int str2StartIndex, int numChars)
• Boolean regionMatches(boolean
ignoreCase,int startIndex, String str2, int
str2StartIndex, int numChars)
import java.io.*;
public class Test{
public static void main(String args[])
{
String Str1 = new String("Welcome to Tutorialspoint.com");
String Str2 = new String("Tutorials");
String Str3 = new String("TUTORIALS");
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(11, Str2, 0, 9));
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(11, Str3, 0, 9));
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(true, 11, Str3, 0, 9));
}
}
Output
Return Value :true
Return Value :false
Return Value :true
• startsWith() - determines whether a
givenStringbegins with a specified string
• endsWith( )determines whether theStringin
question ends with a specified string.
boolean startsWith(String str)
boolean endsWith(String str)
boolean startsWith(String str, int startIndex)
• equals( ) Versus ==
• ==operator compares two object references
•
•
•
•
•
String s1 = "Java";
String s2 = "Java";
String s3 = new string ("Java");
test(Sl == s2) //true
test(s1 == s3) //false
• compareTo() - method compares values and
returns an int which tells if the string
compared is less than, equal to or greater
than the other string. Its general syntax is,
• int compareTo(String str)
String s1 = "Abhi";
String s2 = "Viraaj";
String s3 = "Abhi";
s1.compareTo(S2); //return -1 because s1 < s2
s1.compareTo(S3); //return 0 because s1 == s3
s2.compareTo(s1); //return 1 because s2 > s1
Searching strings
• indexOf( ) Searches for the first occurrence of a character
or substring.
• lastIndexOf( ) Searches for the last occurrence of a
character or substring.
• int indexOf(int ch)
• int lastIndexOf(int ch)
• int indexOf(String str)
• int lastIndexOf(String str)
• int indexOf(int ch, int startIndex)
• int lastIndexOf(int ch, int startIndex)
• int indexOf(String str, int startIndex)
• int lastIndexOf(String str, int startIndex)
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " + "to come to the aid of their country.";
System.out.println(s);
System.out.println("indexOf(t) = " + s.indexOf('t'));
System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
System.out.println("indexOf(the) = " +s.indexOf("the"));
System.out.println("lastIndexOf(the) = " +s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " +s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " +s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " +s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " +s.lastIndexOf("the", 60));
}
}
Now is the time for all good men to come to the aid of their country.
indexOf(t) = 7
lastIndexOf(t) = 65
indexOf(the) = 7
lastIndexOf(the) = 55
indexOf(t, 10) = 11
lastIndexOf(t, 60) = 55
indexOf(the, 10) = 44
lastIndexOf(the, 60) = 55
Modifying a String
• substring() method returns a part of the string.
substring() method has two forms,
public String substring(int begin);
public String substring(int begin, int end);
• concat( ) - concatenate two strings using concat( ),
String concat(Stringstr)
• replace() method replaces occurrences of character with
a specified new character.
String str = "Change me";
System.out.println(str.replace('m','M'));
• trim() - This method returns a string from which any
leading and trailing whitespaces has been removed.
String str = " hello "; System.out.println(str.trim());
Data Conversion Using valueOf( )
• ThevalueOf( )method converts data from its
internal format into a human-readable form.
Changing the Case of Characters Within a String
• String toLowerCase( )
• String toUpperCase( )
StringBuffer
• StringBuffer is a peer class of String that provides much of the
functionality of strings.
• String represents fixed-length, immutable character sequences.
• In contrast, StringBuffer represents growable and writeable character
sequences.
• StringBuffer may have characters and substrings inserted in the middle or
appended to the end.
• StringBuffer will automatically grow to make room for such additions and
often has more characters pre-allocated than are actually needed, to allow
room for growth.
• StringBuffer Constructors
• StringBuffer defines these four constructors:
–
–
–
–
StringBuffer( )
StringBuffer(intsize)
StringBuffer(Stringstr)
StringBuffer(CharSequencechars)
• length( ) and capacity( )
• The current length of a StringBuffer can be found via
the length( )method, while the total allocated
capacity can be found through the capacity(
)method.
– int length( )
– int capacity( )
• ensureCapacity( )
• If you want to pre-allocate room for a certain
number of characters after a StringBuffer has been
constructed, you can use ensureCapacity( )to set the
size of the buffer.
– void ensureCapacity(intcapacity)
• setLength( ) - To set the length of the buffer within a
StringBuffer object.
– void setLength(int len)
• charAt( ) – Obtain single character
– char charAt(int where)
• setCharAt( ) – Set the value of a character
– void setCharAt(int where, char ch)
• getChars( ) - To copy a substring of a StringBuffer into
an array.
– void getChars(int sourceStart, int sourceEnd, chartarget[
],int targetStart)
• append( ) - concatenates 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)
• insert( ) - inserts one string into another.
– StringBuffer insert(int index, String str)
– StringBuffer insert(int index, char ch)
– StringBuffer insert(int index, Object obj)
• reverse( ) - reverse the characters
– StringBuffer reverse( )
• delete( ) - deletes a sequence of characters from the invoking
object.
– StringBuffer delete(intstartIndex, intendIndex)
•
deleteCharAt( ) - deletes the character at the index specified by
loc. It returns the resulting StringBuffer object
– StringBuffer deleteCharAt(intloc)
• replace( ) - replace one set of characters with another set
– StringBuffer replace(intstartIndex, intendIndex, Stringstr)
• substring( ) - obtain a portion of a StringBuffer by
callingsubstring( ).
– String substring(intstartIndex)
– String substring(intstartIndex, intendIndex