Java Examples - Comparing two strings
Problem Description:
How to compare two strings ?
Solution:
Following example compares two strings by using str compareTo (string) , str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings .
public class StringCompareEmp{
public static void main(String args[]){
String str = "Hello World";
String anotherString = "hello world";
Object objStr = str;
System.out.println( str.compareTo(anotherString) );
System.out.println( str.compareToIgnoreCase(anotherString) );
System.out.println( str.compareTo(objStr.toString()));
}
}
Result:
The above code sample will produce the following result.
-32
0
0
Java Examples - Searching last occurance ?
Problem Description:
How to search the last position of a substring ?
Solution:
This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,Hello Reader";
int lastIndex = strOrig.lastIndexOf("Hello");
if(lastIndex == - 1){
System.out.println("Hello not found");
}else{
System.out.println("Last occurrence of Hello
is at index "+ lastIndex);
}
}
}
Result:
The above code sample will produce the following result.
Last occurrence of Hello is at index 13
Java Examples - Deleting a character
Problem Description:
How to remove a particular character from a string ?
Solution:
Following example shows hoe to remove a character from a particular position from a string with the help of removeCharAt(string,position) method.
public class Main {
public static void main(String args[]) {
String str = "this is Java";
System.out.println(removeCharAt(str, 3));
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
}
Result:
The above code sample will produce the following result.
thi is Java
Java Examples - Replacing a String
Problem Description:
How to replace a substring inside a string by another one ?
Solution:
This example describes how replace method of java String class can be used to replace character or substring by new one.
public class StringReplaceEmp{
public static void main(String args[]){
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
Result:
The above code sample will produce the following result.
Wello World
Wallo World
Hallo World
Java Examples - String Reverse
Problem Description:
How to reverse a String?
Solution:
Following example shows how to reverse a String after taking it from command line argument .The program buffers the input String using StringBuffer(String string) method, reverse the buffer and then converts the buffer into a String with the help of toString() method.
public class StringReverseExample{
public static void main(String[] args){
String string="abcdef";
String reverse = new StringBuffer(string).
reverse().toString();
System.out.println("\nString before reverse:
"+string);
System.out.println("String after reverse:
"+reverse);
}
}
Result:
The above code sample will produce the following result.
String before reverse:abcdef
String after reverse:fedcba
Java Examples - Search in a String
Problem Description:
How to search a word inside a string ?
Solution:
This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found. Otherwise it returns -1.
public class SearchStringEmp{
public static void main(String[] args) {
String strOrig = "Hello readers";
int intIndex = strOrig.indexOf("Hello");
if(intIndex == - 1){
System.out.println("Hello not found");
}else{
System.out.println("Found Hello at index "
+ intIndex);
}
}
}
Result:
The above code sample will produce the following result.
Found Hello at index 0
Java Examples - Spliting a String
Problem Description:
How to split a string into a number of substrings ?
Solution:
Following example splits a string into a number of substrings with the help of str split(string) method and then prints the substrings.
public class JavaStringSplitEmp{
public static void main(String args[]){
String str = "jan-feb-march";
String[] temp;
String delimeter = "-";
temp = str.split(delimeter);
for(int i =0; i < temp.length ; i++){
System.out.println(temp[i]);
System.out.println("");
str = "jan.feb.march";
delimeter = "\\.";
temp = str.split(delimeter);
}
for(int i =0; i < temp.length ; i++){
System.out.println(temp[i]);
System.out.println("");
temp = str.split(delimeter,2);
for(int j =0; j < temp.length ; j++){
System.out.println(temp[j]);
}
}
}
}
Result:
The above code sample will produce the following result.
jan
feb
march
jan
jan
feb.march
feb.march
jan
feb.march
No comments:
Post a Comment