// Use String.valueOf()
String s = String.valueOf(true); // true
s = String.valueOf('a'); // a
s = String.valueOf((short)111); // 111
s = String.valueOf(111); // 111
s = String.valueOf(111L); // 111
s = String.valueOf(1.11F); // 1.11
s = String.valueOf(1.11D); // 1.11
Tuesday, June 23, 2009
How to Convert a String to Upper or Lower Case
7:57 PM
Posted by
Techsavy
// Convert to upper case
String upper = “abc”.toUpperCase(); // ABC
// Convert to lower case
String lower = “ABC”.toLowerCase(); // abc
String upper = “abc”.toUpperCase(); // ABC
// Convert to lower case
String lower = “ABC”.toLowerCase(); // abc
How to Search a String for a Character or a Substring
7:57 PM
Posted by
Techsavy
String string = "Java Java";
// First occurrence of a character ‘a’
int index = string.indexOf('a'); // 1
// First occurrence of a sting “dam”
index = string.indexOf("ava"); // 1
// First occurrence of a character ‘a’
int index = string.indexOf('a'); // 1
// First occurrence of a sting “dam”
index = string.indexOf("ava"); // 1
How to Get a Substring from a String
7:56 PM
Posted by
Techsavy
int start = 1;
int end = 3;
String substr = "Java".substring(start, end); // av
int end = 3;
String substr = "Java".substring(start, end); // av
How to Construct a String
7:56 PM
Posted by
Techsavy
StringBuffer buf = new StringBuffer("Hi");
// Append
buf.append(" Welcome");
buf.append(“ To Java”);
// Convert to string
String s = buf.toString(); // Hi Welcome To Java
// Append
buf.append(" Welcome");
buf.append(“ To Java”);
// Convert to string
String s = buf.toString(); // Hi Welcome To Java
How to Compare Strings
7:55 PM
Posted by
Techsavy
String s1 = "a";
String s2 = "A";
// Check if identical
boolean b = s1.equals(s2); // false
// Check if identical ignoring case
b = s1.equalsIgnoreCase(s2); // true
StringBuffer sbuf = new StringBuffer("a");
b = s1.contentEquals(sbuf); // true
String s2 = "A";
// Check if identical
boolean b = s1.equals(s2); // false
// Check if identical ignoring case
b = s1.equalsIgnoreCase(s2); // true
StringBuffer sbuf = new StringBuffer("a");
b = s1.contentEquals(sbuf); // true
How to Construct a String From StringBuffer
7:54 PM
Posted by
Techsavy
StringBuffer buf = new StringBuffer("Hi");
// Append
buf.append(" Welcome");
// Convert StringBuffer to string
String s = buf.toString();
// Append
buf.append(" Welcome");
// Convert StringBuffer to string
String s = buf.toString();
How to Get the Package of a Class
7:54 PM
Posted by
Techsavy
Class cls = java.lang.String.class;
Package packageOfClass = cls.getPackage();
String packageName = packageOfClass.getName(); // java.lang
Package packageOfClass = cls.getPackage();
String packageName = packageOfClass.getName(); // java.lang
How to List the Interfaces That a Class Implements
7:53 PM
Posted by
Techsavy
Class cls = java.lang.String.class;
Class[] interfaces = cls.getInterfaces();
Class[] interfaces = cls.getInterfaces();
How to Get the Superclass of an Object
7:52 PM
Posted by
Techsavy
Object o = new String();
Class superClass = o.getClass().getSuperclass(); // java.lang.Object
Class superClass = o.getClass().getSuperclass(); // java.lang.Object
How to Determine If a Class Object Represents a Class or Interface
7:52 PM
Posted by
Techsavy
Class cls = java.lang.String.class;
boolean isClass = cls.isInterface(); // false
cls = java.lang.Cloneable.class;
isClass = cls.isInterface(); // true
boolean isClass = cls.isInterface(); // false
cls = java.lang.Cloneable.class;
isClass = cls.isInterface(); // true
How to Get the Name of a Class Object
7:50 PM
Posted by
Techsavy
Class cls = java.lang.String.class;
String name = cls.getName();
String name = cls.getName();
How to Get an Object of the Class
7:49 PM
Posted by
Techsavy
There are three ways to retrieve a class object.
1) Class cls = object.getClass();
2)
try {
cls = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
}
3) cls = java.lang.String.class;
1) Class cls = object.getClass();
2)
try {
cls = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
}
3) cls = java.lang.String.class;
Subscribe to:
Posts (Atom)