String handling:
String is a
collection of character. In java, String defines the objects. The string object
is used in order to handle some text.
Most widely used
functions are,
1. The length() function:
It is used to count and return the number of
characters presents.
Example program length():
import
java.util.Scanner;
public class
String_length {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any
string: ");
String a = sc.next();
System.out.print("Length of "+a+" is :
"+a.length()+" Characters \n");
}
}
Output:
2. String compare:
Method 1:
import
java.util.Scanner;
public class
String_equals {
public static void main(String args[]) {
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any
string: ");
String a = sc.next();
String str="Whereisstuff";
if (a.equals(str))
{
System.out.println("Entered
string "+a+" is equal to stored string("+str+").");
}
else
{
System.out.println("Entered
string "+a+" is not equal to stored
string("+str+").");
}
}
}
Method 2:
import
java.util.Scanner;
public class
String_compare_method2 {
public static void main(String args[]) {
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any
string: ");
String a = sc.next();
String str="Whereisstuff";
if (a==str)
{
System.out.println("Enterd string
"+a+" is equal to stored string("+str+").");
}
else
{
System.out.println("Enterd
string "+a+" is not equal to stored
string("+str+").");
}
}
}
Output:
If condition:
Else condition:
3. String compare with ignore case:
import
java.util.Scanner;
public class
String_EqualsIgnore {
public static void main(String args[]) {
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any
string: ");
String a = sc.next();
String str="Whereisstuff";
if (a.equalsIgnoreCase(str))
{
System.out.println("Enterd
string "+a+" is equal to stored string("+str+").");
}
else
{
System.out.println("Enterd
string "+a+" is not equal to stored
string("+str+").");
}
}
}
Output:
If condition:
Else condition:
4. String to compare with array:
import
java.util.Scanner;
public class
String_Compare_in_Array {
public static void main(String[] args) {
int[] num = {1000, 2000, 3000, 4000};
boolean found = false;
Scanner input = new Scanner(System.in);
System.out.println("Enter your PIN
number : ");
int pin = input.nextInt();
for (int n : num) {
if (n == pin) {
found = true;
break;
}
}
if (found) {
System.out.println("Entered
PIN number is correct.");
} else {
System.out.println("Sorry try
again!!!");
}
}
}
Output:
5. String upper and lower case:
import
java.util.Scanner;
public class
String_upper_lower {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any string
in capital letters: ");
String a = sc.next();
System.out.print("Entered string
is converted upper into lower : "+a.toLowerCase()+"\n");
System.out.print("Enter any string
in small letters: ");
String b = sc.next();
System.out.print("Entered string
is converted lower into upper : "+b.toUpperCase()+"\n");
}
}
Output: