Search Java Programs

Wednesday, January 20, 2010

Java String valueOf Example

String class valueOf method example.


String class valueOf method example:- This example demonstrates the working of valueOf method. this method returns String representations of all data types values

Syntax:-valueOf(boolean b)

Here is the code:-

 /**
* @(#) ValueOfString.java
* ValueOfString class demonstrates the working of valueOf() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/



public class ValueOfString {
public static void main(String args[]) {

// method converts any data type value in to String type
boolean result = true;
int in = 234;
char ch = 'u';
double dou = 34.86;
float flt = 54.889f;

System.out.println("String represented boolean value: "
+ String.valueOf(result)
+ "\nString represented integer value: " + String.valueOf(in)
+ "\nString represented char value: " + "'"
+ String.valueOf(ch) + "'"
+ "\nString represented double value: " + String.valueOf(dou)
+ "\nString represented float value: " + String.valueOf(flt));

}
}
Output of the program:-
String represented boolean value: true
String represented integer value: 234
String represented char value: 'u'
String represented double value: 34.86
String represented float value: 54.889

Java String lastIndexOf Example

String class lastIndexOf method example.


String class lastIndexOf method examle:- This example demonstrates the working of lastIndexOf method. This method returns the index or number location of letter from last of the statement.

Syntax:-lastIndexOf(String str)

Here is the code:-

 /**
* @(#) LastIndexOfString.java
* LastIndexOfString class demonstrates the working of lastIndexOf() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/


public class LastIndexOfString {
public static void main(String args[]) {

String prabhu = "o paalan haari", bholenaath;
// double quotes as arguments inside parentheses
// Method returns last index of letter or word passes in single ang
System.out.println("last index of letter 'i': "
+ prabhu.lastIndexOf('i'));

bholenaath = "bam bolo bam bolo bam bam bam";
// here instead of index of word 'bolo', the letter which word stats
// that is 'b', is returned and same happens every time for every word
// or statement
System.out.println("Index of letter 'b' is returned: "
+ bholenaath.lastIndexOf("bolo"));
// here were complete statement is passed in the arguments but still
// methos returns the index of first letter only
System.out.println("Index of letter 'o' is returned: "
+ prabhu.lastIndexOf("o paalan haari"));
}
}
Output of the program:-
last index of letter ' i ': 13
Index of letter 'b' is returned: 13
Index of letter 'o' is returned: 0

Java String equalsIgnoreCase Example

String class equalsIgnoreCase method example


String class equalsIgnoreCase method example:- This method demonstrates the working of equalsIgnoreCase method. this method returns a boolean value after examining the two String. this method's examines without considering case senstivity

Syntax:-equalsIgnoreCase(String anotherString)

Here is the code:-

/**
* @(#) EqualsIgnoreCaseString.java
* EqualsIgnoreCaseString class demonstrates the working of equalsIgnoreCase() method of String class of lang package
* @version 15-May-2008
* @author Rose India Team
*/



public class EqualsIgnoreCaseString {
public static void main(String args[]) {

String str = "koi mil gaya", str1 = "Koi mil gaya", str2 = "";
boolean result = str.equalsIgnoreCase(str1);
// Method compares two String leaving case Senstivity behind
//Strings str and str1 are equal therefore method here returns true
System.out.println("method returns true: " + result);

String str3 = new String("12234");

result = str.equalsIgnoreCase(str3);
//Strings str and str3 are not equal
System.out.println("false is returned: " + result);
}
}
Output of the program:-
method returns true: true
false is returned: false

Website Design by Mayuri Multimedia