Strings In Java
Strings In Java
String: String is a collection of characters (OR) It can also be defined as a character array. String is a predefined class in java which contains methods variables . These are immutable in nature (Once an object is created cant be changed).
Syntax:
Initialization of String:
String variable[]={'char1','char2','char3'.......};
String object="value";
String object="value";
Example:
String st={'v','o','i','d'};
String obj="void";
String obj="void";
Declaration of String:
String can be declared in many ways as follows :
String str[];
Example:
String str[];
Using compareTo Method: 9 (Str1>Str2)
Using compareToIgnoreCase Method: 9 (Str1>Str2).
StringBuffer Class: It is also same as String class but mutable (Objects of String Buffer Class can be modified or can be changed) . This can execute only one thread at a time , hence called as Synchronized class .
Syntax:
StringBuffer object=new StringBuffer();
or
StringBuffer Object=new StringBuffer(String);
or
StringBuffer Object=new StringBuffer(int capacity);
String str[];
Example:
String str[];
Predefined Methods In String Class
1. length(): It is used to return the length of a string . The return value of length method is an integer.
Syntax:
object.length();
2. charAt(int position): It is used to return a character at a specified of position in the parameter. Position is a integer parameter which will be considered as index of the String.
Syntax:
object.charAt(integer value);
3.equals(String variable): It is used to return a boolean value . if both the strings are equal and are of same case then it returns true otherwise false.
Syntax:
object.equals(string object);
4.equalsIgnoreCase(String variable): It is used to return a boolean value without checking the case values of the string . If both the strings are equal then ot returns true otherwise falls.
Syntax:
object.equalsIgnoreCase(String object2);
5.toUpper(): It is used to convert the string to upper case letters . If the given String is already in upper case then it ignores that string or a character.
Syntax:
objet.toUpper();
3.equals(String variable): It is used to return a boolean value . if both the strings are equal and are of same case then it returns true otherwise false.
Syntax:
object.equals(string object);
4.equalsIgnoreCase(String variable): It is used to return a boolean value without checking the case values of the string . If both the strings are equal then ot returns true otherwise falls.
Syntax:
object.equalsIgnoreCase(String object2);
5.toUpper(): It is used to convert the string to upper case letters . If the given String is already in upper case then it ignores that string or a character.
Syntax:
objet.toUpper();
6.toLower(): It is used to convert the string to Lower case letters . If the given String is already in Lower case then it ignores that string or a character.
Syntax:
objet.toUpper();
7. concat(String Variable): It is used for combining two Strings together forming into a single String. The return type of concat method is also a String.
Syntax:
object.concat(object2);
8.trim(): It is used to remove the spaces present before the String and after the String. The return type of trim method is String.
Syntax:
object.trim();
9.compareTo(String variable): It is used to compare the String lexicographically , It returns positive negative or 0 . if first String is greater than the second String it returns positive value . if first String is lesser than the second String it returns negative value . If both are equal then returns 0.
Syntax:
object.compareTo(object2);
10.compareToIgnoreCase(String variable); It is used to compare the String lexicographically , It returns positive negative or 0 . if first String is greater than the second String it returns positive value . if first String is lesser than the second String it returns negative value . If both are equal then returns 0. It checks even based on the Case Values .
Syntax:
object.compareToIgnoreCase(object2);
11.contains(String): It is used to search the sequence of characters in a given String. If the sequence is present returns true otherwise false .
Syntax:
object.contains("Character to be searched");
12.endsWith(String): It is used to check weather the sequence of character is ended in the given string or not . If the given character is equal to the end value of the String then this method returns true otherwise falls.
Syntax:
object.endsWith("character Sequence");
13.indexOf("String or a character"): It is used to return the index of the given character or a String present in the String.
Syntax:
object.indexOf("character or a String");
14.isEmpty(): It is used to return a boolean value . If the String is empty it returns true otherwise false.
Syntax:
object.isEmpty();
15.replace(charSequence old , charequence new): It is used to replace the one character Sequence with other character Sequence.
Syntax:
object.replace(old characterSequence,new CharacterSequence);
import java.util.*;
class StringMethods
{
public static void main(String[] args)
{
System.out.println("Enter A String value");
Scanner sc=new Scanner(System.in);
String str1=sc.next();
System.out.println("Using Length Method:"+str1.length());
System.out.println("Using CharAt Method:"+str1.charAt(2));
System.out.println("Using toUpper Method:"+str1.toUpperCase());
System.out.println("Using toLower Method:"+str1.toLowerCase());
System.out.println("Using trim Method:"+str1.trim());
System.out.println("Using isEmpty Method:"+str1.isEmpty());
System.out.println("Using indexOf Method:"+str1.indexOf("M"));
System.out.println("Using endsWith Method:"+str1.endsWith("n"));
System.out.println("Using replace Method:"+str1.replace("VoidMain","Void Main Technologies"));
System.out.println("Using Contains Method:"+str1.contains("Void"));
}
}
Using CharAt Method: i
Using toUpper Method: VOIDMAIN
Using toLower Method: voidmain
Using trim Method: VoidMain
Using isEmpty Method: false
Using indexOf Method: 4
Using endsWith Method: true
Using replace Method: voidMainTechnologies
Using Contains Method: true
import java.util.*;
class StringMethods1
{
public static void main(String[] args)
{
System.out.println("Enter two String values");
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
System.out.println("Using equals Method:"+str1.equals(str2));
System.out.println("Using concat Method:"+str1.concat(str2));
System.out.println("Using compareTo Method:"+str1.compareTo(str2));
System.out.println("Using compareToIgnoreCase Method:"+str1.compareToIgnoreCase(str2));
}
}
Using equals Method: false
Using concat Method: VoidMain
Syntax:
objet.toUpper();
7. concat(String Variable): It is used for combining two Strings together forming into a single String. The return type of concat method is also a String.
Syntax:
object.concat(object2);
8.trim(): It is used to remove the spaces present before the String and after the String. The return type of trim method is String.
Syntax:
object.trim();
9.compareTo(String variable): It is used to compare the String lexicographically , It returns positive negative or 0 . if first String is greater than the second String it returns positive value . if first String is lesser than the second String it returns negative value . If both are equal then returns 0.
Syntax:
object.compareTo(object2);
10.compareToIgnoreCase(String variable); It is used to compare the String lexicographically , It returns positive negative or 0 . if first String is greater than the second String it returns positive value . if first String is lesser than the second String it returns negative value . If both are equal then returns 0. It checks even based on the Case Values .
object.compareToIgnoreCase(object2);
11.contains(String): It is used to search the sequence of characters in a given String. If the sequence is present returns true otherwise false .
Syntax:
object.contains("Character to be searched");
12.endsWith(String): It is used to check weather the sequence of character is ended in the given string or not . If the given character is equal to the end value of the String then this method returns true otherwise falls.
Syntax:
object.endsWith("character Sequence");
13.indexOf("String or a character"): It is used to return the index of the given character or a String present in the String.
Syntax:
object.indexOf("character or a String");
14.isEmpty(): It is used to return a boolean value . If the String is empty it returns true otherwise false.
Syntax:
object.isEmpty();
15.replace(charSequence old , charequence new): It is used to replace the one character Sequence with other character Sequence.
Syntax:
object.replace(old characterSequence,new CharacterSequence);
Program using String methods
class StringMethods
{
public static void main(String[] args)
{
System.out.println("Enter A String value");
Scanner sc=new Scanner(System.in);
String str1=sc.next();
System.out.println("Using Length Method:"+str1.length());
System.out.println("Using CharAt Method:"+str1.charAt(2));
System.out.println("Using toUpper Method:"+str1.toUpperCase());
System.out.println("Using toLower Method:"+str1.toLowerCase());
System.out.println("Using trim Method:"+str1.trim());
System.out.println("Using isEmpty Method:"+str1.isEmpty());
System.out.println("Using indexOf Method:"+str1.indexOf("M"));
System.out.println("Using endsWith Method:"+str1.endsWith("n"));
System.out.println("Using replace Method:"+str1.replace("VoidMain","Void Main Technologies"));
System.out.println("Using Contains Method:"+str1.contains("Void"));
}
}
Save As:
StringMethods.java
Compile:
javac StringMethods.java
Run:
java StringMethods
Input:
Enter A String values
VoidMain
Output:
Using Length Method: 8
Using toUpper Method: VOIDMAIN
Using toLower Method: voidmain
Using trim Method: VoidMain
Using isEmpty Method: false
Using indexOf Method: 4
Using endsWith Method: true
Using replace Method: voidMainTechnologies
Using Contains Method: true
Program using Some more String methods
import java.util.*;
class StringMethods1
{
public static void main(String[] args)
{
System.out.println("Enter two String values");
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
System.out.println("Using equals Method:"+str1.equals(str2));
System.out.println("Using concat Method:"+str1.concat(str2));
System.out.println("Using compareTo Method:"+str1.compareTo(str2));
System.out.println("Using compareToIgnoreCase Method:"+str1.compareToIgnoreCase(str2));
}
}
Save As:
StringMethods.java
Compile:
javac StringMethods.java
Run:
java StringMethods
Input:
Enter two String values
Void
Main
Main
Output:
Using equals Method: false
Using concat Method: VoidMain
Using compareTo Method: 9 (Str1>Str2)
Using compareToIgnoreCase Method: 9 (Str1>Str2).
StringBuffer Class: It is also same as String class but mutable (Objects of String Buffer Class can be modified or can be changed) . This can execute only one thread at a time , hence called as Synchronized class .
Syntax:
StringBuffer object=new StringBuffer();
or
StringBuffer Object=new StringBuffer(String);
or
StringBuffer Object=new StringBuffer(int capacity);
Predefined Methods In StringBuffer Class
1.append(String): it is used to combine two StringBuffers together. These method returns String values . The overloaded methods for append are append(int) , append(char) , append(double) , append(float) , append(short int).
Syntax:
Object.append("String value");
2.insert(int,String): It is used to insert the String or a character at a specified position . These method returns a String value . It takes to parameters
- Integer for specifying position to insert .
- String or character to be inserted .
Syntax:
Object.insert(position,"String value");
3.reverse(): It is used for reversing the given String . It returns the String value .
Syntax:
object.reverse();
4.capacity(): It is used to specify the capacity of a StringBuffer . It returns a integer value in bits format.
Syntax:
object.capacity();
5.ensureCapacity(int): It is used to create a StringBuffer atleast equal to specified value . The value changes as (old capacity*2)+2.
Syntax:
object.ensureCapacity(int value);
6.length(): It is used to return the length of the StringBuffer . It returns integer value .
Syntax:
Object.length();
7.replace(startIndex , endIndex , String): It is used to replace the String value specified between starting and ending index. It returns String value.
Syntax:
Object.replace(int,int,"String value");
8.delete(StartIndex , EndIndex): It is used to delete the String present between the Starting and Ending Index .
Syntax:
Object.delete(int,int);
Program using StringBuffer methods
import java.util.*;
class StringBufferMethods
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER TWO STRING VALUES");
String s1=sc.next();
String s2=sc.next();
StringBuffer str1=new StringBuffer(s1);
StringBuffer str2=new StringBuffer(s2);
System.out.println("Using append Method:"+str1.append(str2));
System.out.println("Using insert Method:"+str1.insert(4,"M"));
System.out.println("Using reverse Method:"+str1.reverse());
System.out.println("Using capacity Method:"+str1.capacity());
System.out.println("Using length Method:"+str1.length());
System.out.println("Using replace Method:"+str1.replace(0,7,"VOIDMAIN"));
System.out.println("Using delete Method:"+str1.delete(0,3));
}
}
Save As:
StringBufferMethods.java
Compile:
javac StringBufferMethods.java
Run:
java StringBufferMethods
Input:
ENTER TWO STRING VALUES:
VoidMain
Technologies
Output:
Using append Method: voidMainTechnologies
Using insert Method:VoidMMainTechnologies
Using reverse Method:seigolonhectniaMMdioV
Using capacity Method:24
Using length Method: 21
Using replace Method: VOIDMAINnhectniaMMdiov
Using delete Method:DMAINnhectniaMMdiov.
Note:
Once String performs an operation then the original String is replaced with the String which is obtained as an output . Then the replaced String is used for the further operations.
Example:
From the above Program :
str1=VoidMain , after performing the append operation str1 changed to str1=VoidMainTechnologies . Hence the changed str1 is used for the further operations .
3.StringBuilder Class: String Builder class is same as the String class but here multiple threads can execute at a time . These are also imputable objects . The Main difference between the String and String Builder class are Strings can execute only one thread at a time whereas String Buffer can execute multiple Threads at a time.
Syntax:
StringBuilder sb=new String Builder();
OR
StringBuilder sb=new StringBuilder(String);
OR
StringBuilder sb=new StringBuilder(capacity);
Predefined Methods In StringBuilder Class
1.append(String): it is used to combine two StringBuffers together. These method returns String values . The overloaded methods for append are append(int) , append(char) , append(double) , append(float) , append(short int).
Syntax:
Object.append("String value");
2.insert(int,String): It is used to insert the String or a character at a specified position . These method returns a String value . It takes to parameters
- Integer for specifying position to insert .
- String or character to be inserted .
Syntax:
Object.insert(position,"String value");
3.reverse(): It is used for reversing the given String . It returns the String value .
Syntax:
object.reverse();
4.capacity(): It is used to specify the capacity of a StringBuffer . It returns a integer value in bits format.
Syntax:
object.capacity();
5.ensureCapacity(int): It is used to create a StringBuffer atleast equal to specified value . The value changes as (old capacity*2)+2.
Syntax:
object.ensureCapacity(int value);
6.length(): It is used to return the length of the StringBuffer . It returns integer value .
Syntax:
Object.length();
7.replace(startIndex , endIndex , String): It is used to replace the String value specified between starting and ending index. It returns String value.
Syntax:
Object.replace(int,int,"String value");
8.delete(StartIndex , EndIndex): It is used to delete the String present between the Starting and Ending Index .
Syntax:
Object.delete(int,int);
class StringBuilderMethods
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER TWO STRING VALUES");
String s1=sc.next();
String s2=sc.next();
StringBuilder str1=new StringBuilder(s1);
StringBuilder str2=new StringBuilder(s2);
System.out.println("Using append Method:"+str1.append(str2));
System.out.println("Using insert Method:"+str1.insert(4,"M"));
System.out.println("Using reverse Method:"+str1.reverse());
System.out.println("Using capacity Method:"+str1.capacity());
System.out.println("Using length Method:"+str1.length());
System.out.println("Using replace Method:"+str1.replace(0,7,"VOIDMAIN"));
System.out.println("Using delete Method:"+str1.delete(0,3));
}
}
Program using StringBuilder methods
import java.util.*;class StringBuilderMethods
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER TWO STRING VALUES");
String s1=sc.next();
String s2=sc.next();
StringBuilder str1=new StringBuilder(s1);
StringBuilder str2=new StringBuilder(s2);
System.out.println("Using append Method:"+str1.append(str2));
System.out.println("Using insert Method:"+str1.insert(4,"M"));
System.out.println("Using reverse Method:"+str1.reverse());
System.out.println("Using capacity Method:"+str1.capacity());
System.out.println("Using length Method:"+str1.length());
System.out.println("Using replace Method:"+str1.replace(0,7,"VOIDMAIN"));
System.out.println("Using delete Method:"+str1.delete(0,3));
}
}
Save As:
StringBufferMethods.java
Compile:
javac StringBuilderMethods.java
Run:
java StringBuilderMethods
Input:
ENTER TWO STRING VALUES:
VoidMain
Technologies
Output:
Using append Method: voidMainTechnologies
Using insert Method:VoidMMainTechnologies
Using reverse Method:seigolonhectniaMMdioV
Using capacity Method:24
Using length Method: 21
Using replace Method: VOIDMAINnhectniaMMdiov
Using delete Method:DMAINnhectniaMMdiov.
String Builder class supports both the methods of String Buffer and String class.
4.StringTokenizer class: String Tockenizer is used to divide the String into tokens .
Syntax:
StringTokenizer st=new StringTokenizer(Srting);
OR
StringTokenizer st=new StringTokenizer(String , deli-meter);
OR
StringTokenizer st=new StringTokenizer(String , deli-meter, flag);
Predefined Methods In StringTokenizer Class
1.nextToken(): These method returns the next Token in the given String .
Syntax:
Object.nextToken();
2.nextToken(deli-meter): These method is used to return the Token based on the deli-meter.
Syntax:
Object.nextToken(deli-meter);
3.hasMoreToken(); These method is used to return the boolean value . If more tokens are present returns true otherwise false.
Syntax:
Object.hasMoreTokens();
4.hasMoreElements(); These method returns a boolean value . If it has more elements returns true otherwise false.
Syntax:
Object.hasMoreElements();
5.nextElement(): These method is used to return a boolean value . If the element is present returns true otherwise false.
Syntax:
Object.nextElement();
Program using StringTokenizer methods
import java.util.*;
class StringTokenizerMethods
{
public static void main(String[] args)
{
System.out.println("String Tokenizer Methods");
Scanner sc=new Scanner(System.in);
System.out.println("ENTER STRING VALUE");
String s1=sc.next();
StringTokenizer str1=new StringTokenizer(s1,",");
System.out.println("Using nextToken Method:"+str1.nextToken());
System.out.println("Using nextTocken with deli-meter:"+str1.nextToken(","));
System.out.println("Using hasMoreTockens Method:"+str1.hasMoreTokens());
System.out.println("Using hasMoreElements Method:"+str1.hasMoreElements());
}
}
Save As:
StringTokenizerMethods.java
Compile:
javac StringTokenizerMethods.java
Run:
java StringTokenizerMethods
Input:
String Tokenizer Methods
ENTER STRING VALUE:
VoidMain,Technologies
Using nextTocken with deli-meter:Technologies
Using hasMoreTockens Method:false
hasMoreElements Method:false
Comments
Post a Comment