package Strings;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
public class StringDemo
{
public static void main(String args[])
{
String s1 = "abc";
String s2 = new String("abc");
String s3="asdf";
System.out.println("s1 hash code = " + s1.hashCode());
System.out.println("s2 hash code = " + s2.hashCode());
System.out.println("s3 hash code = " + s3.hashCode());
if(s1==s2)
{
System.out.println("yes A");
}
else
System.out.println("yes B");
if(s1.equals(s2))
{
System.out.println("yes c");
}
else
System.out.println("yes D");
String test = "ZMJW";
for (int i = 0; i < test.length(); ++i)
{
char c = test.charAt(i);
System.out.println((int) c);
}
int ints[] = new int[3];
Object objs[] = new Object[7];
String stra = "Hello World";
String strb = new String();
System.out.println("Length of args is " + args.length);
System.out.println("Length of ints is " + ints.length);
System.out.println("Length of objs is " + objs.length);
System.out.println("Length of stra is " + stra.length());
System.out.println("Length of strb is " + strb.length());
System.out.println("Java Strings in action:");
System.out.println("An alarm entered in Octal: \007");
System.out.println("A tab key: \t(what comes after)");
System.out.println("A newline: \n(what comes after)");
System.out.println("A UniCode character: \u0207");
System.out.println("A backslash character: \\"+"\\");
String str1 ="\\" ;
String str2 = str1+str1;
System.out.println(str2);
System.out.println(str1);
String str3 = " \\asdfgfhstr1gfgfgfgfg ";
String str4 =str3.replace("\\", str2);
System.out.println(str4);
String one = "A String";
String two = "A String";
String three = new String(one);
compare(one, two);
compare(two, three);
String helloHtml = "" +
"" +
"
"" +
"Hello, today is: " + new Date() +
"" +
"";
String title = StringUtils.substringBetween(helloHtml, "
String content = StringUtils.substringBetween(helloHtml, "", "");
System.out.println("title = " + title);
System.out.println("content = " + content);
String one1 = "";
String two1 = "\t\r\n";
String three1 = " ";
String four = null;
String five = "four four two";
// We can use StringUtils class for checking if a string is empty or not
// using StringUtils.isBlank() method. This method will return true if
// the tested string is empty, contains white space only or null.
System.out.println("Is one empty? " + StringUtils.isBlank(one1));
System.out.println("Is two empty? " + StringUtils.isBlank(two1));
System.out.println("Is three empty? " + StringUtils.isBlank(three1));
System.out.println("Is four empty? " + StringUtils.isBlank(four));
System.out.println("Is five empty? " + StringUtils.isBlank(five));
// On the other side, the StringUtils.isNotBlank() methods complement
// the previous method. It will check is a tested string is not empty.
System.out.println("Is one not empty? " + StringUtils.isNotBlank(one));
System.out.println("Is two not empty? " + StringUtils.isNotBlank(two));
System.out.println("Is three not empty? " + StringUtils.isNotBlank(three));
System.out.println("Is four not empty? " + StringUtils.isNotBlank(four));
System.out.println("Is five not empty? " + StringUtils.isNotBlank(five));
String var1 = null;
String var2 = "";
String var3 = " \t\t\t";
String var4 = "Hello World";
System.out.println("var1 is blank? = " + StringUtils.isBlank(var1));
System.out.println("var2 is blank? = " + StringUtils.isBlank(var2));
System.out.println("var3 is blank? = " + StringUtils.isBlank(var3));
System.out.println("var4 is blank? = " + StringUtils.isBlank(var4));
System.out.println("var1 is not blank? = " + StringUtils.isNotBlank(var1));
System.out.println("var2 is not blank? = " + StringUtils.isNotBlank(var2));
System.out.println("var3 is not blank? = " + StringUtils.isNotBlank(var3));
System.out.println("var4 is not blank? = " + StringUtils.isNotBlank(var4));
System.out.println("var1 is empty? = " + StringUtils.isEmpty(var1));
System.out.println("var2 is empty? = " + StringUtils.isEmpty(var2));
System.out.println("var3 is empty? = " + StringUtils.isEmpty(var3));
System.out.println("var4 is empty? = " + StringUtils.isEmpty(var4));
System.out.println("var1 is not empty? = " + StringUtils.isNotEmpty(var1));
System.out.println("var2 is not empty? = " + StringUtils.isNotEmpty(var2));
System.out.println("var3 is not empty? = " + StringUtils.isNotEmpty(var3));
System.out.println("var4 is not empty? = " + StringUtils.isNotEmpty(var4));
String source = "this is a test";
String word = "is";
int wordFound = StringUtils.countMatches(source, word);
System.out.println(wordFound);
}
public static void compare(String one, String two)
{
System.out.println("Comparing...");
if (one == two)
{
System.out.println("Strings are shared: " +
one.hashCode() + ", " + two.hashCode());
}
else if (one.equals(two))
{
System.out.println("At least the strings are equal: " + one.hashCode() + ", " + two.hashCode());
System.out.println((Object)one);
System.out.println(two);
}
else System.out.println("This is rather distressing, sir.");
System.out.println();
}
}
No comments:
Post a Comment