// Get a system property
String dir = System.getProperty("SampleProperty");
// Set a system property
String previousValue = System.setProperty("SampleProperty", "newValue");
Wednesday, June 24, 2009
Tuesday, June 23, 2009
How to Convert a Primitive Type Value to a String
7:58 PM
Posted by
Techsavy
// 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
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
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
Subscribe to:
Posts (Atom)