Hello friends how are you, today in this blog i will teach you what is String function, types of String function, how String functions work and many programs using String functions in a very simple way.
Let's start
Here we are going to learn all String functions one by one
1 toUpperCase
- It is used to convert all the lowercase alphabet into uppercase alphabet.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.toUpperCase()); } } /* ### OUTPUT ### EASY */
- 2 toLowerCase
- It is used to convert all the uppercase alphabet into lowercase alphabet.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.toLowerCase()); } } /* ### OUTPUT ### easy */
- 3 length
- It is used to count the total number of character in string.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.length()); } } /* ### OUTPUT ### 4 because total number of character is 4 */
- 4 charAt
- It is used to get single character at particular index.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.charAt(2)); } } /* ### OUTPUT ### s indexing starts from 0 so at index 2 s is present */
- 5 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 returns false.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.startsWith("ea"));//false System.out.println(s.startsWith("Ea"));//true } } /* ### OUTPUT ### false true */
- 6 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 returns false.
class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.endsWith("sy"));//true System.out.println(s.endsWith("sY"));//false } } /* ### OUTPUT ### true false */
- 7 compareTo
- It is used to compare two string.
- It returns zero or non-zero value.
- If the both string are same returns zero otherwise returns non-zero.
//Example 1 class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.compareTo("Easy")); System.out.println(s.compareTo("easy")); } } /* ### OUTPUT ### 0 -32 */
//Example 2:Login Program import java.util.Scanner; class EASY { 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("abcd@gmail.com")==0&&password.compareTo("12345")==0) System.out.println("Login Success"); else System.out.println("Login Failed!!!"); } } /* ### OUTPUT ### Enter username abcd@gmail.com Enter password 12345 Login Success */
- 8 equals
- It is used to compare two string.
- It returns boolean value(true/false).
- If the both string are same returns true otherwise returns false.
//Example 1 class EASY { public static void main(String[] args) { String s="Easy"; System.out.println(s.equals("Easy")); System.out.println(s.equals("easy")); } } /* ### OUTPUT ### true false */
//Example 2:Login Program import java.util.Scanner; class EASY { 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(); //using equals function if(username.equals("abcd@gmail.com")&&password.equals("12345")) System.out.println("Login Success"); else System.out.println("Login Failed!!!"); } } /* ### OUTPUT ### Enter username abcd@gmail.com Enter password 12345 Login Success */
- 9 toCharArray
- It is used to convert the string into character array.
//Example 1 class EASY { public static void main(String[] args) { String s="Easy"; 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 E character at 1 is a character at 2 is s character at 3 is y */
- 10 reverse
- It is used to reverse the character of string.
- reverse function is defined inside StringBuffer.
class EASY { public static void main(String[] args) { String s="Easy"; String rev=new StringBuffer(s).reverse().toString(); System.out.println("Original String="+s+"\nReverse String="+rev); } } /* ### OUTPUT ### Original String=Easy Reverse String=ysaE */
- 11 replace
- It is used to replace the particular string with new string.
/* Syntax: replace(old char,new char) */ class EASY { public static void main(String[] args) { String s="Easy softwares is telecom training centre"; //replace telecom with software String s1=s.replace("telecom", "software"); System.out.println("Original String="+s+"\nUpdated String="+s1); } } /* ### OUTPUT ### Original String=Easy softwares is telecom training centre Updated String=Easy softwares is software training centre */
- 12 replaceAll
- It is used to replace all the matching string with new string.
/* Syntax: replaceAll(old char,new char) */ class EASY { public static void main(String[] args) { String s="Easy softwares is software training centre"; //replace all a with @ String s1=s.replaceAll("a", "@"); System.out.println("Original String="+s+"\nUpdated String="+s1); } } /* ### OUTPUT ### Original String=Easy softwares is software training centre Updated String=E@sy softw@res is softw@re tr@ining centre */
- 13 replaceFirst
- It replaces only first substring of the string that matches with new string.
/* Syntax: replaceFirst(old char,new char) */ class EASY { public static void main(String[] args) { String s="Easy softwares is software training centre"; //replace only first a with @ String s1=s.replaceFirst("a", "@"); System.out.println("Original String="+s+"\nUpdated String="+s1); } } /* ### OUTPUT ### Original String=Easy softwares is software training centre Updated String=E@sy softwares is software training centre */
- 14 getBytes
- It is used to get the ASCII value of each alphabet,digit and special symbol of string.
/* ASCII value of A=65,B=66,C=67...Z=90 a=97,b=98,c=99...z=122 */ class EASY { public static void main(String[] args) { String s="Easy"; //passing string into byte array byte ar[]=s.getBytes(); for (int i = 0; i < ar.length; i++) { System.out.println(ar[i]); } } } /* ### OUTPUT ### 69 97 115 121 */
- 15 indexOf
- It is used to get the index of particular character.
- Suppose that the given character exists more than one time in the given string then indexOf function returns the index of letter which is first from the left.
class Easy { public static void main(String[] args) { String s="Easy Softwares"; System.out.println(s.indexOf("a")); } } /* ### OUTPUT ### 1 because indexing starts from 0 */
- 16 lastIndexOf
- Suppose that the given character exists more than one time in the given string then lastIndexOf function returns the index of letter which is last from the left.
class Easy { public static void main(String[] args) { String s="Easy Softwares"; System.out.println(s.lastIndexOf("a")); } } /* ### OUTPUT ### 10 because indexing starts from 0 */
- 17 trim
- It is used to remove unwanted spaces from the string.
class Easy { public static void main(String[] args) { String s=" Easy"; System.out.println("Before Trim"); System.out.println(s); System.out.println("After Trim"); System.out.println(s.trim()); } } /* ### OUTPUT ### Before Trim Easy After Trim Easy */
- 18 contains
- It is used to match the specified sequence of character in the given string.
- It returns boolean value(True/False).
- If the given character matches with string returns true otherwise returns false.
class Easy { public static void main(String[] args) { String s="Easy softwares is an ISO Certified Training Centre"; System.out.println(s.contains("ISO")); } } /* ### OUTPUT ### true */
- 19 intern
- It is used to copy one string into another.
class Easy { public static void main(String[] args) { String s="Easy softwares"; System.out.println(s); String s2=s.intern(); System.out.println(s2); } } /* ### OUTPUT ### Easy softwares Easy softwares */
- 20 valueOf
- It is used to convert data type into string.
class Easy { public static void main(String[] args) { int no=10; System.out.println(no+20); String s=String.valueOf(no); System.out.println(s+20); } } /* ### OUTPUT ### 30 1020 */
- 21 isEmpty
- It is used to check given string is empty or not.
- It returns boolean value(True/False).
class Easy { public static void main(String[] args) { String s1="Easy"; System.out.println(s1.isEmpty()); String s2=""; System.out.println(s2.isEmpty()); } } /* ### OUTPUT ### false true */
- 22 substring
- It returns the substring for given begin index.
class Easy { public static void main(String[] args) { String s1="EasySoftwares"; //substring(startIndex) System.out.println(s1.substring(4)); //Softwares //substring(startIndex,endString) System.out.println(s1.substring(4,8));//Soft } } /* ### OUTPUT ### Softwares Soft */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.😊
0 Comments