What Will I Learn?
This tutorial covers following topic
- String Class in Java
- Use String class methods in Java. Methods are concat() , toUpperCase() , toLowerCase() , trim() , startsWith() , endsWith() , replace() . charAt() , length() , valueOf(). Rest of the method will discuss in second part.
Requirements
-OS: Windows/Linux/mac
-Preinstalled Jvm
-Preinstalled Jdk
-Preinstalled Code editor.
Difficulty
You must have knowledge of simple java program writing
- Intermediate
Tutorial Contents
In this tutorial I will tell you about String class and uses of String class methods
String Class
In java, String is a class which gives us a lot of methods to perform operation on string. Some of the methods are concat() , equals() , split() , length() etc.
java.lang.String is the name of String class.This call implements three interfaces. Serializable , compareble and CharSequence interface.
CharSequence interfaces represents sequence of charecter. It is implemented by three classes of String, StringBuilder , StringBuffer. Means we can handle string in java by using three classes.
But in this tutorial I will show you handle string with String class.
Java String class is immutable which can not be change further. It means if we change something in string then a new instance is created. It simply means unchangeble. If a string object is created then we can not change or modify data and state but we can create new object.
Example-
class Civilstudy{ public static void main(String args[]){ String a="Steemit"; a.concat("Decentralized");//concat() method appends the string at the end System.out.println(a);//will print Sachin because strings are immutable objects } }Output-
SteemitFrom the above example it is clear that here Steemit is not change but object is created with SteemitDecentralized. This is reason string is immutable.BUILD SUCCESSFUL (total time: 0 seconds)
How to create String objects-
There are two ways to create and object in Java.
- String literals
- New keywords.
String literals:-
This is created using double quote.
Example-
String a1="Steemit" String a2="Steemit" //will not create new instance
Every time when we create a string literal JVM checks and if that string value is already exist in pool then it will not create new object but will return the reference to the same instances.
New Keywords:-
Example-
String a=new String("Steemit");//creates two objects and one reference variableJvm creates new string objects in normal heap memory. And string literals goes to constant pool. The varriable a refers to use object in heap.
Methods of String class-
Java provides many methods to perform operation on string. We will discuss here methods concat() , toUpperCase() , toLowerCase() , startsWith() , endsWith() , charAt() , valureOf() , replace() , trim() , length() .
Concatination-
In java there is two ways of concatination of string.
Using +operator-
Concatenation operator + is used in this process.
Example-
class Civilstudy{ public static void main(String args[]){Output-String a="Steemit"+" Decentralized";
System.out.println(a);//Steemit Decentralized
}
}
Steemit DecentralizedUsing concat() method-BUILD SUCCESSFUL (total time: 0 seconds)
This method concate one string to the end of other string.
Example-
class Civilstudy{ public static void main(String args[]){Output-String a1="Steemit ";
String a2="Decentralized";String a3=a1.concat(a2);
System.out.println(a3);//Steemit Decentralized
}
}
Steemit DecentralizedBUILD SUCCESSFUL (total time: 0 seconds)
toUpperCase() method -
This method is used to convert all letters of string in uppercase.
Example-
public class Civilstudy{ public static void main(String args[]){Output-String a="Steemit";
System.out.println(a.toUpperCase());//STEEMITSystem.out.println(a);//Steemit(no change in original)
}
}
STEEMITWe can see it display the string in uppercase but it not convert it actual data value.Steemit
BUILD SUCCESSFUL (total time: 0 seconds)
toLowerCase() method-
This method is used to convert all letters of string in lowercase.
Example-
public class Civilstudy{ public static void main(String args[]){Output-String a="Steemit";
System.out.println(a.toLowerCase());//steemit
System.out.println(a);//Steemit(no change in original)
}
}
steemitWe can see it display the string in lowercase but it not convert it actual data value.Steemit
BUILD SUCCESSFUL (total time: 0 seconds)
trim() method -
It is use to remove white spaces before and after any string.
Example-
public class Civilstudy{ public static void main(String args[]){Output-String sa=" Steemit ";
System.out.println(a);// Steemit
System.out.println(a.trim());//Steemit
}
}
SteemitSteemit
BUILD SUCCESSFUL (total time: 0 seconds)
We can see it removes white spaces when we trim.
startsWith() method-
This method is boolean which returns true or false value. This method is used to check wether an string is starts with particuler substring or not.
endswith() method-
This method is also return true or false value.This method is used to check wether an string is ends with particuler substring or not.
Example-
public class Civilstudy{ public static void main(String args[]){String a="Steemit";
System.out.println(a.startsWith("St"));//true
System.out.println(s.endsWith("t"));//true
}
}
Output- true trueBUILD SUCCESSFUL (total time: 0 seconds)
charAt() method-
This method is used to find a perticuler letter at particuler index value.
Example-
public class Civilstudy{ public static void main(String args[]){Output-String a="Steemit";
System.out.println(a.charAt(0));//chatrecter at index position 0
System.out.println(a.charAt(3));//chatrecter at index position 0
}
}
Slength() method-e
BUILD SUCCESSFUL (total time: 0 seconds)
This is used for counting the length of string.
Example-
public class Civilstudy{ public static void main(String args[]){Output-String a="Steemit";
System.out.println(a.length());//Counting of the length of string
}
}
6BUILD SUCCESSFUL (total time: 0 seconds)
String ValueOf() method-
This method converts data type int, float , double , long , boolean ,char , chararray etc into string
Example-
public class Civilstudy{ public static void main(String args[]){Output-int a=10;
String a1=String.valueOf(a);
System.out.println(a1+10);
}
}
1010Here we can see it treated as String because it concatenates the value instead add the value.BUILD SUCCESSFUL (total time: 0 seconds)
replace() method-
This method is used for replace the charecter with given charecter.
Example-
public class Civilstudy{ public static void main(String args[]) { String a1="Steemit is decentralized platform."; String aString=a1.replace("platform","website");//replaces all occurrences of "platform" to "website" System.out.println(aString); } }Output-
Steemit is decentralized website.Here we can see it replaces platform with website.BUILD SUCCESSFUL (total time: 0 seconds)
####Conclusion
In this tutorial we learn about String class and some of its methods.
What will next?
In the next tutorial we show the use of other importent method of String class which is not discuss in this tutorial.
Curriculum
This is first part of Java String.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit