Replace (char oldchar,char newchar) replace (Stirng oldString, String newString) :
By using above method we are replacing the particular character of the String. And particular portion of the string.
string replace program
class Test
{
public static void main(String[] args)
{
String str="hi freetimelearn";
System.out.println(str.replace('h','H'));
System.out.println(str.replace("learn","l"));
}
}
Output :
Hi freetimelearn,hi freetimes
endsWith() and startsWith():-
endsWith() is used to find out if the string is ending with particular character/string or not.
startsWith() used to find out the particular String starting with particular character/string or not.
String endsWith and startsWith
class Test
{
public static void main(String[] args)
{
String str="freetimelearn";
System.out.println(str.endsWith("n"));//true
System.out.println(str.endsWith("hi"));//false
System.out.println(str.startsWith("d"));//false
System.out.println(str.startsWith("f"));//true
}
}
Output :
substring(int startingposition) & substring(int startingposition,int endingposition):-
By using above method we are getting substring from the whole String.
In the above methods
starting position parameter value is including
ending position parameter value is excluding
Substring :
class sup
{
public static void main(String[] args)
{
String str="freetimelearn";
System.out.println(str.substring(2));
System.out.println(str.substring(1,7));
System.out.println("ratansoft".substring(2,5));
}
}
Output :