Home | | Sitemap ||Page number :25

Extracting Characters from a String

A String is made of several individual characters. A character can be an alphabet, a digit or even a space. We can easily extract a single or multiple character(s) from a String. The methods used are —

  1. charAt();
  2. getChars();

charAt()

The charAt() method is used to extract a single character from a String.

Example--

String xyz=“ Perfect ”; char c=xyz.charAt(1); The charAt() method will extract the first character of the String and will initialize the variable “c” with it. If you imagined the first character of the String to be ‘P’, think again! The characters of String start with 0, so the first character will be ‘e’.

The complete program.. class extch { public static void main(String args[]) { String xyz="Perfect";

char c=xyz.charAt(1); System.out.println("The character extracted is "+c); } }

a

getChars()

The getChars() method extracts multiple characters from the String.

Syntax—

getChars(int StartPos, int EndPos, TargetArrayString, int StartArrayIndex);

Let us suppose that we have a string “Networking”. You have to extract four characters “work” from the string. As we have multiple characters to extract and store, a single character variable won’t be able to hold them all. Thus a character’s array is needed (An Array of characters can store as many characters as you wish!) class extch2 { public static void main(String args[]) { String xyz="Networking"; char ch[]=new char[5]; xyz.getChars(3,7,ch,0); System.out.println(ch); } }

getBytes()

The getBytes() method extracts String characters as bytes and stores it in a character array.

Syntax--

getBytes(int StartPos, int EndPos, TargetArrayString, int StartArrayIndex);

class extBytes {

public static void main(String args[]) { String abc="Networking"; byte b[]=new byte[5]; abc.getBytes(3,7,b,0); System.out.println(b); } }

wewe

At the time of compilation you might get this error— Note:: filename.java uses or overrides a deprecated API. Note:: Recompile with -Xlint:deprecation for details.

This shows that this program uses an API(Application Programming Interface) that is deprecated (No longer popularly used), hence this error is flashed. You can ignore this and simply execute the program.

toCharArray()

The toCharArray() method is used to convert a Strings content entirely into an array.

class arrycon { public static void main(String args[]) { String name="Mobiles"; char c[]=new char[8]; c=name.toCharArray(); System.out.println(c); } }

we

substring() method

The substring method is used to extract some specified characters of a String and create a new String from these characters. As an example, consider a String ‘str’ initialized with the word “Penguin”. If we write-- new1=str.substring(5); If you will print the String “new1”, the output will be “in”.

s

What will you do to extract “Pen” as a String from Penguin. You will have to supply two parameters to the substring() method.

class substr { public static void main(String args[]) { String stv="Penguin"; String new1; new1=stv.substring(0,3); System.out.println("Contents of the new String are "+new1); } }

sdf

Remember: To extract “Pen” out of “Penguin”, the index you need to specify are….. For P-0 For n-3 Index number of n is “2” but still we need to specify “3”. This is not so in case of “P”, which is the first number.

trim()

The trim() method is used to remove spaces in a String from both of its ends. class trm { public static void main(String args[]) { String jkl=" Welcome to India"; jkl=jkl.trim(); System.out.println(jkl); } }

sdsd

Changing the case of Alphabets

You can change the case of alphabets easily from lower to uppercase and vice-versa using toLowerCase() & toUpperCase().

class case1 { public static void main(String args[]) { String name="bill gates "; name=name.toUpperCase(); System.out.println(name); } }

sdf

class case2 { public static void main(String args[]) { String name="COMPUTER APPLICATION"; System.out.println(name.toLowerCase());

} }

xcxcwe

equals()

The equals() method is used to check if true Strings are identical. You should keep in mind that “hello” and “HELLO” are not identical as their cases are different.

class eql { public static void main(String args[]) { String str="name"; String str2="Name"; if(str.equals(str2)) System.out.println("The two Strings are equal"); else System.out.println("The two Strings are unequal"); } }

sdf

If you don’t want to consider the difference of Case, the method “equalsIgnoreCase()” should be used.

class eql { public static void main(String args[]) { String str="name"; String str2="Name"; if(str.equalsIgnoreCase(str2)) System.out.println("The two Strings are equal"); else System.out.println("The two Strings are unequal"); } }

sdfsd