String Methods in Java
Introduction :
The java String Methods provides a lot of built in methods that are used to manipulate string in java. We can perform operations on string object such as shorting, concatenating, trimming, replacing, appending etc.
Let's use some important methods of string class.
1. Java String to UpperCase
- It is used to convert all the lowercase alphabet into UPPERCASE alphabet.
- Let's see an example for better understanding.
// 1. toUpperCase Methods
class StrUP{
public static void main(String args[]){
String s="technology";
System.out.println(s.toUpperCase());
}
}
||||||||OUTPUT||||||||
BLOG> javac blog.java
BLOG> java StrUP
TECHNOLOGY
2. Java String to LowerCase
- It is used to convert all the LOWERCASE alphabet into uppercase alphabet.
- Let's see an example for better understanding.
// 2. toLowerCase Methods
class StrLP{
public static void main(String args[]){
String s="LOWERCASE TECHNOLOGY";
System.out.println(s.toLowerCase());
}
}
||||||||OUTPUT||||||||
lowercase technology
3. Java String to length
- It is used to count the total number of character in string.
- Let's see an example for better understanding.
// 3. length Methods
class StrL{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.length());
}
}
||||||||OUTPUT||||||||
10
//because total number of character is 10
4. Java String charAt
- It is used to get single character at particular index.
- Let's see an example for better understanding.
// 4. charAt Methods
class StrCA{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.charAt(4));
}
}
||||||||OUTPUT||||||||
N
//indexing starting from 0 so at index 4 of place N is present
5. Java String startsWith
- It is used to check prefix of a string.
- It returns boolean value(true/false).
- If the given string begins with specified letter returns true otherwise return false.
- Let's see an example for better understanding.
// 5. startsWith Methods
class StrSW{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.startsWith("te")); //false
System.out.println(s.startsWith("TE")); //true
}
}
||||||||OUTPUT||||||||
false
true
6. Java String endsWith
- It is used to check the given string is ending with specified word or not.
- It returns boolean value(true/false).
- If the given string ends with specified letter returns true otherwise return false.
- Let's see an example for better understanding.
// 5. endsWith Methods
class StrEW{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.endsWith("te")); //false
System.out.println(s.endsWith("GY")); //true
}
}
||||||||OUTPUT||||||||
false
true
7. Java String compareTo
- It is used to compare two string.
- It is return zero or non-zero value.
- It returns boolean value(true/false).
- If the both string are same returns zero otherwise returns zero otherwise returns non-zero.
- Let's see an example for better understanding.
// 7.compareTo Methods
// Example 1.
class StrCT{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.compareTo("technology")); //false
System.out.println(s.compareTo("TECHNOLOGY")); //true
}
}
||||||||OUTPUT||||||||
-32
0
Q1. Find a program using compareTo String methods create login page.
import java.util.Scanner;
class StrCT2{
public static void main(String args[]){
Scanner in= new Scanner(System.in);
String username,password;
System.out.println("Enter Email ID:");
username=in.next();
System.out.println("Enter Password:");
password=in.next();
if(username.compareTo("shailes@gmail.com")==0
&password.compareTo("1234567")==0){
System.out.println("Login Success");
}
else{
System.out.println("Login Failed");
}
}
}
||||||||OUTPUT||||||||
..// Fail Process
Enter Username:
Shailesh
Enter Password:
78954
Login Failed
...//Success Process
Enter Username:
shailes@gmail.com
Enter Password:
1234567
Login Success
8. Java String equals
- It is used to compare two string.
- It returns boolean value(true/false).
- If the given string two or more than are same then returns true otherwise returns false.
- Let's see an example for better understanding.
// 8. equals Methods
// Example 1.
class StrEQ{
public static void main(String args[]){
String s="TECHNOLOGY";
System.out.println(s.equals("technology")); //false
System.out.println(s.equals("TECHNOLOGY")); //true
}
}
||||||||OUTPUT||||||||
false
true
Q1. Find a program using equals String methods create login page.
// 8. equals Methods
import java.util.Scanner;
class StrEQ{
public static void main(String args[]){
Scanner in= new Scanner(System.in);
String username,password;
System.out.println("Enter Email ID:");
username=in.next();
System.out.println("Enter Password:");
password=in.next();
if(username.equals("shailes@gmail.com") && password.equals("1234567")){
System.out.println("Login Success__");
}
else{
System.out.println("Login Failed !!!");
}
}
}
||||||||OUTPUT||||||||
..// Fail Process
Enter Username:
Shailesh
Enter Password:
78954
Login Failed
// try again for success login
9. Java String charArray
- It is used to convert into string to character.
- Let's see an example for better understanding.
// 9. toCharArray Methods
class StrCArr{
public static void main(String arg[]){
String s= "Technology";
char ch[]=s.toCharArray();
for(int i=0; i<ch.length; i++){
System.out.println("Character at "+"[" +i+ "]" + "is="+ch[i]);
}
}
}
||||||||OUTPUT||||||||
Character at [0]is=T
Character at [1]is=e
Character at [2]is=c
Character at [3]is=h
Character at [4]is=n
Character at [5]is=o
Character at [6]is=l
Character at [7]is=o
Character at [8]is=g
Character at [9]is=y
10. Java String reverse
- It is used to reverse the character all.
- reverse function is defined inside StringBuffer
- Let's see an example for better understanding.
// 10. reverse Methods
class StrRV{
public static void main(String arg[]){
String s= "Technology";
String rev= new StringBuffer(s).reverse().toString();
System.out.println("Origninal String Value is= "+s);
System.out.println("Reverse String Value is= "+rev);
}
}
||||||||OUTPUT||||||||
Origninal String Value is= Technology
Reverse String Value is= ygolonhceT
11. Java String replace
- It is used to replace the particular with new string.
- Let's see an example for better understanding.
// 11. replace Methods
class StrRP{
public static void main(String arg[]){
String s= "Technology369kk is blog provided tech post"; // main value
String s1= s.replace("blog","websites");
System.out.println("The Orginal value is="+s);
System.out.println("The Update/replace value is="+s1);
}
}
||||||||OUTPUT||||||||
The Orginal value is=Technology369kk is blog provided tech post
The Update/replace value is=Technology369kk is websites provided tech post
12. Java String replaceall
- It is used to replace all the matching string with new string .
- Let's see an example for better understanding.
// 12. replaceall Methods
// Syntax:
// replaceAll(old char, new char);
class StrRPALL{
public static void main(String arg[]){
String s= "Technology369kk is blog provided tech post";
// replace all place 'o' 's';
String s1= s.replaceAll("o","s");
System.out.println("The Orginal value is="+s);
System.out.println("The Update/replace value is="+s1);
}
}
||||||||OUTPUT||||||||
The Orginal value is=Technology369kk is blog provided tech post
The Update/replace value is=Technslsgy369kk is blsg prsvided tech psst
13. Java String replacefirst
- It is used to replaces only first subString of the string that matches with new string.
- Let's see an example for better understanding.
// 13. replace first Methods
class StrRPF{
public static void main(String arg[]){
String s= "TECHNOLOGY369KK"; // main value
String s1= s.replaceFirst("T","t");
System.out.println("The Orginal value is="+s);
System.out.println("The Update/replace value is="+s1);
}
}
||||||||OUTPUT||||||||
The Orginal value is=TECHNOLOGY369KK
The Update/replace value is=tECHNOLOGY369KK
14. Java String getBytes
- It is used to get the ASCII value of each alphabet, digit, and special symbol of string.
- Let's see an example for better understanding.
// HINTS:
// ASCII value of:
// Capital Letter :A=65, B=66, C=67 ......Z=90.
// Small Letter :a=97, b=98, c=99 ......z=122.
class StrGB{
public static void main(String arg[]){
String s= "ABCDEF";
// passing string into bytes arrays
byte ar[]=s.getBytes();
for(int i=0; i<ar.length; i++){
System.out.println("ASCII value is="+ar[i]);
}
}
}
||||||||OUTPUT||||||||
ASCII value is=65
ASCII value is=66
ASCII value is=67
ASCII value is=68
ASCII value is=69
ASCII value is=70
15. Java String indexOf
- It is used to get the index of particular character.
- Suppose that the given character exits more than one time in the given string then indexOf function return the index of letter of letter which is first from the left.
- Let's see an example for better understanding.
// 15. indexOf Methods
class StrIX{
public static void main(String args[]){
String s= "Technology Blog";
System.out.println(s.indexOf("n"));
}
}
||||||||OUTPUT||||||||
4
16. Java String lastindexOf
- It is used to in arrays find location.
- Suppose that the given character exits more than one time in the given string then lastindexOf function return the index of letter of letter which is last from the left.
- Let's see an example for better understanding.
// 16. lastindexOf Methods
class StrLIX{
public static void main(String args[]){
String s= "Technology Blog";
//here last arrays value of index blog of o then
// so index start from 0,1,2...
System.out.println(s.lastIndexOf("o"));
}
}
||||||||OUTPUT||||||||
13
17. Java String trim
- It is used to used to remove unwanted space from the string.
- Let's see an example for better understanding.
// 17. trim Methods
class StrTR{
public static void main(String args[]){
String s= " Technology";
System.out.println("Before trim="+s);
System.out.print("After trim=");
System.out.println(s.trim());
}
}
||||||||OUTPUT||||||||
Before trim= Technology
After trim=Technology
18. Java String contains
- It is used to match the specified sequence of character in the given string ,
- It return value boolean (true/false).
- If the given character matches with string returns otherwise returns false.
- Let's see an example for better understanding.
// 18. contains Methods
class StrCO{
public static void main(String args[]){
String s= "Technology369kk is free of cost blog certified
with google adsenes";
System.out.println(s.contains("blog"));
}
}
||||||||OUTPUT||||||||
true
19. Java String intern
- It is used to used to copy one string into another.
- Let's see an example for better understanding.
// 19. intern Methods
class StrIN{
public static void main(String args[]){
String s= "Technology369kk is free of cost blog certified with google adsenes";
System.out.println("Orginal Value is:"+s);
String s1=s.intern();
System.out.println("Copy all Character of s="+s1);
}
}
||||||||OUTPUT||||||||
Orginal Value is:Technology369kk is free of cost blog certified with google adsenes
Copy all Character of s=Technology369kk is free of cost blog certified
with google adsenes
20. Java String valueOf
- It is used to convert data type into string.
- Let's see an example for better understanding.
// 20. valueOf Methods
class StrVF{
public static void main(String args[]){
int s=3;
System.out.println("current value="+s+6);
String s1=String.valueOf(s);
System.out.println("Convet value in string ="+(s1+69));
}
}
||||||||OUTPUT||||||||
current value=36
Convet value in string =369
21. Java String isEmpty
- It is used to check the given number is string empty or not.
- It is return value boolean type(true/ false).
- Let's see an example for better understanding.
// 21. isEmpty Methods
class StrEM{
public static void main(String args[]){
String s="Technology Lover";
System.out.println(s.isEmpty()); //Not because tech Lover is written
String s1=""; // Don't use any space
System.out.println(s1.isEmpty()); //Yes because this blank
}
}
||||||||OUTPUT||||||||
true
false
22. Java String substring
- It is used to print particular value help of arrays index.
- It is returns the substring for given begin index with last index number then so answers.
- Let's see an example for better understanding.
// 22. substring Methods
class StrSS{
public static void main(String args[]){
// SubString Start Index from 0 we Known
String s="Technology Lover"; //Lover
System.out.println(s.substring(10));
// SubString Startindex and EndIndex value
System.out.println(s.substring(10, 15)); //Love because add space 1
}
}
|||||||OUTPUT||||||||
Lover
Love
I hope this post is helpful for you learn string methods
Comments