Java Strings

String Class

In Java a string is a class rather than a character array. In other words, a character array is different from string in Java.

A string is an object with the set of characters. No null indicator at the end of the string object. A string class provides a set of methods that handles strings.

The following is the set of methods in Strings:

For Example,

public class StringDemo{     public static void main(String args[]){          String p=args[0];
String q=args[1];
String r = p;
if(p.equalsIgnoreCase(q))
System.out.println("Same")
else
System.out.println("Different")

if(p==q)
System.out.println("Same")
else
System.out.println("Different")
if(p==r)
System.out.println("Same")
else
System.out.println("Different")
}}

Output:

CSE-A   CSE-A
Same
Different
Same

Note: equals() compares the content of the object where as == compares the object references.

An example of string sort:

import java.util.*;public class StringSort{ public static void main(String args[]){   int n,i,j;
String k[];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of strings");
n=sc.nextInt();
k=new String[n];
System.out.println("Enter Names");
for (i=0;i<n;i++)
k[i]=sc.next();
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(k[i].compareTo(k[j])>0)
{
String temp=k[i];
k[i]=temp;
k[j]=k[i];
}
}
}
System.out.println("After Sorting......");
for(i=0;i<n;i++)
System.out.println(k[i]);
}
}

Output,

Enter Number of strings
5
Enter Names
c cplus java python ruby
After Sorting......
c
cplus
java
python
ruby

Now let us see a program with the string methods to toggle the characters in the given string.

Program to convert the given string into toggle case.

import java.util.*;public class Toggle{ public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char ch;
System.out.println("Enter a String");
String p = sc.next();
for(int i=0;i<p.length();i++)
{
ch=p.charAt(i);
if((ch>=65) &&(ch<=90))
ch=Character.toLowerCase(ch);
else
ch=Character.toUpperCase(ch);
System.out.print(ch);
}
}
}

Output, which we can do on the command prompt.

Enter a String
python
PYTHON

Now let us try to print prefixes and suffixes of the string:

import java.util.*;public class StringsDemo{
public static void main(String[] args) {
char ch;
Scanner sc=new Scanner(System.in);

System.out.println("Enter String");
String p=sc.next();

System.out.println("Prfixes of the string are");
for(int i=0;i<p.length();i++)
System.out.println(p.substring(i));
System.out.println("Sufixes of the string are");
for(int i=0;i<p.length();i++)
System.out.println(p.substring(0,i));
}
}

Output will be,

Enter String
Andhrapradesh
Prfixes of the string are
Andhrapradesh
ndhrapradesh
dhrapradesh
hrapradesh
rapradesh
apradesh
pradesh
radesh
adesh
desh
esh
sh
h
Sufixes of the string areA
An
And
Andh
Andhr
Andhra
Andhrap
Andhrapr
Andhrapra
Andhraprad
Andhraprade
Andhraprades

Now let us check whether the given string is palindrome or not.

import java.util.*;public class Palindrome{

public static void main(String[] args) {

char ch;
Scanner sc=new Scanner(System.in);

System.out.println("Enter String");
String p=sc.next();
String q = new String(p);

System.out.println("Prfixes of the string are");

StringBuffer sb=new StringBuffer(q);
sb.reverse();
if(p.equals(sb.toString()))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}

Output,

E:\Programs\Java>java Palindrome
Enter String
abcdcba
Prfixes of the string are
Palindrome
E:\Programs\Java>java Palindrome
Enter String
abcdef
Prfixes of the string are
Not Palindrome

String Tokenizer:

It divides or splits a string into token(sub strings) based on the given symbol. The symbol should be mentioned in terms of a string. It is present in java.util package. It consists of three methods:

  1. hasMoreTokens()
  2. nextToken()
  3. countTokens()

Let us see this with an example.

Now let us try to divide a sentence into strings.

import java.util.*;public class STDemo{

public static void main(String[] args) {

String msg="Today is a holiday";
StringTokenizer st= new StringTokenizer(msg," "); while(st.hasMoreTokens())
System.out.println(st.nextToken());
}
}

Output will be,

Today
is
a
holiday

--

--