Hi All,
Here I wrote the program for String Class & String manipulation in java using simple examples :)
/*
startsWith() Method
lastIndexOf() Method
replace() Method
replaceAll() Method
replaceFirst() Method
split() Method
hashCode() Method
*/
import java.lang.String.*;
import java.io.*;
public class stringOperations3 {
public static void main(String args[]){
/*String Manipuldation Part-3*/
/* String lastIndexOf() Method
*
* Note:
1. int lastIndexOf(int ch): Returns the index within this string of the last occurrence of the specified character or -1 if the character does not occur.
2. public int lastIndexOf(int ch, int fromIndex): Returns the index of the last occurrence of the character in the character sequence represented by this
object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.
3. public int lastIndexOf(String str): If the string argument occurs one or more times as a substring within this object,
then it returns the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.
4. public int lastIndexOf(String str, int fromIndex): Returns the index within this string of the last occurrence of the specified substring,
searching backward starting at the specified index.
*/
String name="SKI Technology in Technopark";
String cName="no";
System.out.println("Result of lastIndexOf(int ch) Method :"+name.lastIndexOf('T'));
System.out.println("Result of lastIndexOf(int ch, int fromIndex) Method :"+name.lastIndexOf('T',4));
System.out.println("Result of lastIndexOf(String str) Method :"+name.lastIndexOf(cName));
System.out.println("Result of astIndexOf(String str, int fromIndex)Method :"+name.lastIndexOf(cName,15));
/* replace() Method
*
* Note: It returns a string derived from this string by replacing every occurrence of oldChar with newChar.
*/
String val=new String("My Name is skitech");
System.out.println("Result of the replace() Method :"+val.replace('s', 'k'));
System.out.println("Result of the replace() Method :"+val.replace('k', 's'));
/*replaceAll() Method
*
* Note: This method replaces each substring of this string that matches the given regular expression with the given replacement.
*/
String cname="SkiTECH is fantastic";
System.out.println("Result of the replaceAll() Method :"+cname.replaceAll("SkiTECH","Salem"));
System.out.println("Result of the replaceAll() Method :"+cname.replaceAll("(.*)is(.*)","Salem"));
/* replaceFirst() Method
*
* Note: This method replaces the first substring of this string that matches the given regular expression with the given replacement.
*/
String cname1="my company name is Skitech in chennai company";
System.out.println("Result of the replaceFirst() Method :"+cname1.replaceFirst(cname1,"bala"));
System.out.println("Result of the replaceFirst() Method1 :"+cname1.replaceFirst("company","COMPANY"));
System.out.println("Result of the replaceFirst() Method2 :"+cname1.replaceFirst("(.*)company(.*)","Skitech"));
/* split() Method
*
* Note: It returns the array of strings computed by splitting this string around matches of the given regular expression.
*/
System.out.println("Result of split() Method");
String Str = new String("Welcome=to=SKITECH=blog");
for (String res: Str.split("="))
System.out.println(res);
System.out.println("Result of split() Method1");
for(String res1:Str.split("=", 3))
System.out.println(res1);
System.out.println("Result of split() Method2");
for(String res2:Str.split("=", 2))
System.out.println(res2);
/* startsWith() Method
*
* Note: It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
*/
String temp1="welcome to the SKiTECH";
System.out.println("Result of startsWith() Method "+temp1.startsWith("welcome"));
System.out.println("Result of startsWith() Method1 "+temp1.startsWith("to"));
System.out.println("Result of startsWith() Method2 "+temp1.startsWith("the",11));
/* hashCode() Method
*
* Note: This method returns a hash code value for this object.
*/
String str1="Ski technology in technopark";
System.out.println("Result of hashCode() Method "+str1.hashCode());
}
}
OUTPUT:
Result of lastIndexOf(int ch) Method :18
Result of lastIndexOf(int ch, int fromIndex) Method :4
Result of lastIndexOf(String str) Method :22
Result of astIndexOf(String str, int fromIndex)Method :8
Result of the replace() Method :My Name ik kkitech
Result of the replace() Method :My Name is ssitech
Result of the replaceAll() Method :Salem is fantastic
Result of the replaceAll() Method :Salem
Result of the replaceFirst() Method :bala
Result of the replaceFirst() Method1 :my COMPANY name is Skitech in chennai company
Result of the replaceFirst() Method2 :Skitech
Result of split() Method
Welcome
to
SKITECH
blog
Result of split() Method1
Welcome
to
SKITECH=blog
Result of split() Method2
Welcome
to=SKITECH=blog
Result of startsWith() Method true
Result of startsWith() Method1 false
Result of startsWith() Method2 true
Result of hashCode() Method 1464026455
Result of lastIndexOf(int ch, int fromIndex) Method :4
Result of lastIndexOf(String str) Method :22
Result of astIndexOf(String str, int fromIndex)Method :8
Result of the replace() Method :My Name ik kkitech
Result of the replace() Method :My Name is ssitech
Result of the replaceAll() Method :Salem is fantastic
Result of the replaceAll() Method :Salem
Result of the replaceFirst() Method :bala
Result of the replaceFirst() Method1 :my COMPANY name is Skitech in chennai company
Result of the replaceFirst() Method2 :Skitech
Result of split() Method
Welcome
to
SKITECH
blog
Result of split() Method1
Welcome
to
SKITECH=blog
Result of split() Method2
Welcome
to=SKITECH=blog
Result of startsWith() Method true
Result of startsWith() Method1 false
Result of startsWith() Method2 true
Result of hashCode() Method 1464026455
No comments:
Post a Comment