How To Use Java String Function With Examples

How To Use Java String Function With Examples
How To Use Java String Function With Examples


How To Use Java String Function With Examples

String: A String is a sequence of characters. A Java String can be created with single quotes(' ') and double quotes(" "). String Lenght can be started (0).Java String can be created as follows.

String=name;

name=new String('icoderweb')

Or

String=name;

name=new String("icoderweb");

A String is a class but string can be used data types.

like 

String name="icoderweb"

Here String name is a reference and icoderweb is an object.


Important Some Others Programming Language String 

👉C++ String Function 

👉C String Function 

👉Python String Function 


Why is input statement needed twice while making a function on strings in Java?

String Function Many Types in Java Language.

1.ToUpperCase: To Upper, case function can be used to convert all the lowercase alphabet into the uppercase alphabet.


How To Use ToUpperCase Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.toUpperCase()); 




*****OUTPUT*****

ICODERWEB


2.ToLowerCase: To lower case function, convert all the uppercase alphabet into the lowercase alphabet.


How To Use ToLowerCase Function in Java?

class ICODER



public static void main(String[] args) 



String name="ICODERWEB"; 


System.out.println(name.toLowerCase()); 




*****OUTPUT*****

icoderweb


3. Length String: A string Lenght function can be used to count the total number of characters in the string.


How To Use Lenght Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.lenght()); 




*****OUTPUT*****

8


4. charAt. A chartAt function can be used to get the single character at a particular index.


How To Use charAt Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.cahrAt(2)); 




*****OUTPUT*****

o


The index can be started from 0 so index 2 o is present in a string.


5.startsWith: A startsWith Function can be used to check the prefix of a String. It returns the boolean value True and False.
If the given string begins with letter returns True otherwise returns False.


How To Use startsWith Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.startsWith("ci")); 


System.out.println(name.startsWith("ic")); 




*****OUTPUT*****

False


True


6.endsWith: A endsWith function can be used to check the given string is ending with the word or not. It returns the boolean value True and False.

If the given string ends with the letter return True otherwise returns False.


How To Use ends with Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.endsWith("eb")); 


System.out.println(name.endsWith("be")); 




*****OUTPUT*****

True


False 


7.compareTo: A compare function can be used to compare two strings. It can return zero or non-zero values.
If both strings are the same return zero otherwise returns non-zero.


How To Use compareTo Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.compareTo("icoderweb")); 


System.out.println(name.compareTo("Icoderweb")); 




*****OUTPUT*****

0


35


How To Use compareTo Function Login Method in Java?

import java.util.Scanner;


class ICODER



public static void main(String[] args) 



Scanner in=new Scanner(System.in);


String username, password;


System.out.println("Enter Username:");


username=in.next();


System.out.println("Enter Password:");


password=in.next();


if(username.compareTo("icoderweb")==0&&password.compa

reTo("1234567")==0)


System.out.println("Login Successfully");


else


System.out.println("Invalid Username or Password");


}



*****OUTPUT*****

Enter Username:icoderweb


Enter Password:1234567


Login Successfully


8. equals: Equals function can be used to compare two strings.it is the same compare function. It returns the boolean values True and False.

If both storaresthey the returner's true otherwise returns false.


How To Use equals Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


System.out.println(name.equals("icoderweb")); 


System.out.println(name.equals("Icoderweb")); 




*****OUTPUT*****

True 


False


9.toCharArray: To Char, the Array function can be used to convert the string into a character array.


How To Use toCharArray Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb";


char c[]=name.toCharArray();


for(int i=0;i<c.lenght;i++)


System.out.println(name.equals("Characters At"+i+"is: 

"+c[i])); 




*****OUTPUT*****

Character At 0 is: I


Character At 1 is:c


Character At 2 is:o


Character At 3 is:d


Character At 4 is:e


Character At 5 is:r


Character At 6 is:w


Character At 7 is:e


Character At 8 is:b


10.reverse: A reverse string function can be used to reverse the character of the string.
the reverse function is defined inside StringBuffer.


How To Use reverse Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb"; 


String r=new StringBuffer(name).reverse().toString();


System.out.println("Original String:"+name); 


System.out.println("Reverse String:"+r); 





*****OUTPUT*****

Original String:icoderweb 


Reverse String:bewredoci


11. replace: A replace string function can be used to replace the particular string with a new string.


How To Use replace Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb Learn Free Coding "; 


name1=name.replace("Learn","website");


System.out.println("Original String:"+name); 


System.out.println("Update String:"+name1); 




*****OUTPUT*****

Original String:icoderweb Learn Free Coding


Update String:icoderweb website Free Coding


12.replaceAll: A replaceAll string function can be used to replace all the matching strings with new strings.


How To Use replaceAll Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb Learn Free Coding"; 


name1=name.replaceAll("o","●");


System.out.println("Original String:"+name); 


System.out.println("Update String:"+name1); 




*****OUTPUT*****

Original String:icoderweb Learn Free Coding


Update String: ic●der web Learn Free C●ding


13. replace first: A replaces First only the first substring of the string that matches with a new string.


How To Use replace the first Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb Learn Free Coding"; 


name1=name.replaceFirst("a","@");


System.out.println("Original String:"+name); 


System.out.println("Update String:"+name1); 




*****OUTPUT*****

Original String:icoderweb Learn Free Coding


Update String:icoderweb Le@rn Free Coding


14.getBytes: A getBytes string function can be used to get the ASCII value of each alphabet, digit, and special symbol of string.


How To Use getsBytes Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoder";


ar[]=name.getBytes();


for(int i=0;i<a.lenght;i++)


{


System.out.println(a[i]); 



}


}


*****OUTPUT*****

105


99


111


100


101


114


15.indexOf: A indexOf String function can be used to get the index of a particular character.

Let us like that the given character exists more than one time in the given string then indexOf function returns the index of the letter which is first from the left.


How To Use indexOf Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb";


System.out.println(name.indexOf("o"); 



}


*****OUTPUT*****

2


16.lastIndexOf:A last indexOf function can be used value of last Elements.


Let us like that the given character exists more than one time in the given string then lastIndexOf function returns the index of the letter which is last from the left.


How To Use lastindexOf Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb";


System.out.println(name.lastindexOf("b"); 



}


*****OUTPUT*****

8


17. trim: A trim string function can be used to remove unwanted spaces from the string.


How To Use trim Function in Java?

class ICODER



public static void main(String[] args) 



String name="          icoderweb";


System.out.println("Before Trim");


System.out.println(name)


System.out.println("After Trim")


System.out.println(name.trim());



}


*****OUTPUT*****

Before Trim


            icoderweb 


After Trim


icoderweb


18. contains: A contains string function that can be used to match the specified sequence of characters in the given string. It returns the boolean values True and false.


If the given character matches with string returns true otherwise return false.


How To Use contains Function in Java?

class ICODER

public static void main(String[] args) 

String name="icoderweb Learn Website";

System.out.println(name.contains("Learn")); 

}

*****OUTPUT*****
True


19.intern: A intern string function can be used to copy one string into another.


How To Use intern Function in Java?

class ICODER

public static void main(String[] args) 

String name="icoderweb Learn Website";

System.out.println(name);

String name1=name.intern();

System.out.println(name1); 

}

*****OUTPUT*****
icoderweb Learn Website

icoderweb Learn Website


20.valueOf: A valueOf string function can be used to convert data type into a string.


How To Use valueOf Function in Java?

class ICODER



public static void main(String[] args) 



int num=100;


System.out.println(num+100);


String n=Strimg.valueOf(num);


System.out.println(n+100);



}


*****OUTPUT*****

200


100100


21.isEmpty: A isEmpty string function can be used to check given string is empty or not. It returns the boolean value True and False.


How To Use isEmpty Function in Java?

class ICODER



public static void main(String[] args) 



String name1="icoderweb";


System.out.println(name1.isEmpty());


String name2="";


System.out.println(name2.isEmpty());



}


*****OUTPUT*****

False


True


22.substring: A substring function can return the substring for a given begin index.


How To Use substring Function in Java?

class ICODER



public static void main(String[] args) 



String name="icoderweb";


System.out.println(name.substring(4));


System.out.println(name.substring(0,8));



}


*****OUTPUT*****

icode


icoderweb 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.