On the move:

I'm currently moving the following to their own blogs: Unix, HTML for Blogs, Perl & Java.
The links are on the left side.

Thursday, October 9, 2008

Java - Check String 2


public class cks
{
public static void main(String args[])
{
String s1, s2, s3, s4, output;

s1 = new String("x");
s2 = new String("y");
s3 = new String("abc xyz");
s4 = new String("Abc Xyz");

output = "s1 = " + s1 + "\n" +
"s2 = " + s2 + "\n" +
"s3 = " + s3 + "\n" +
"s4 = " + s4 + "\n\n";
//--------------------------------------------
// test with 'equals'
//--------------------------------------------
if (s1.equals("x"))
output += "s1.equals \"x\"\n";
else output += "not s1.equals \"x\"\n";
//--------------------------------------------
// test with '=='
//--------------------------------------------
if (s1 == "x")
output += "s1 == \"x\"\n";
else output += "not s1 == \"x\"\n";
//--------------------------------------------
// test for equality (ignore case)
//--------------------------------------------
if (s3.equalsIgnoreCase(s4))
output += "s3.equalsIgnoreCase(s4)\n";
else output += "not s3.equalsIgnoreCase(s4)\n";
//--------------------------------------------
// test compareTo
//--------------------------------------------
output += "\ns1.compareTo(s2) is " + s1.compareTo(s2) +
"\ns2.compareTo(s1) is " + s2.compareTo(s1) +
"\ns1.compareTo(s1) is " + s1.compareTo(s1) +
"\ns3.compareTo(s4) is " + s3.compareTo(s4) +
"\ns4.compareTo(s3) is " + s4.compareTo(s3) +"\n\n";
//--------------------------------------------
// test regionMatches (case sensitive)
//--------------------------------------------
if (s3.regionMatches(0, s4, 0, 3))
output += "1st 3 letters of s3 & s4 match\n";
else output += "1st 3 letters of s3 & s4 do not match\n";
//--------------------------------------------
// test regionMatches (true = ignore case)
//--------------------------------------------
if (s3.regionMatches(true, 0, s4, 0, 3))
output += "1st 3 letters of s3 & s4 match";
else output += "1st 3 letters of s3 & s4 do not match";

System.out.println(output);
}
}

No comments:

Post a Comment