Permalink
Browse files

Added Google Guava library Strings class demo

  • Loading branch information...
1 parent a6e873f commit 4e5a6f76044c50df577162e622fc32fdf227ee71 unknown committed Dec 2, 2011
Showing with 57 additions and 0 deletions.
  1. +57 −0 Java/GuavaStrings.java
View
@@ -0,0 +1,57 @@
+import com.google.common.base.Strings;
+
+public class GuavaStrings {
+ public static void main( String[] args )
+ {
+ demonstrateEmptyToNull();
+ demonstrateNullToEmpty();
+ demonstrateRepeat();
+ demonstratePadEnd();
+ demonstratePadStart();
+ }
+
+ public static void demonstrateEmptyToNull() {
+ String string1 = "Hey you.";
+ String string2 = "";
+
+ String string3 = Strings.emptyToNull( string1 );
+ System.out.println( "Strings.emptyToNull() called on non-empty string results in the string: " + string3 );
+
+ string3 = Strings.emptyToNull( string2 );
+ System.out.println( "Strings.emptyToNull() called on empty string results in null: " + string3 );
+
+ }
+
+ public static void demonstrateNullToEmpty() {
+ String string1 = "Hey you.";
+ String string2 = null;
+
+ String string3 = Strings.nullToEmpty( string1 );
+ System.out.println( "Strings.nullToEmpty() called on non-empty string results in the string: " + string3 );
+
+ string3 = Strings.nullToEmpty( string2 );
+ System.out.println( "Strings.nullToEmpty() called on null results in emptyString: " + string3 );
+
+ }
+
+ public static void demonstrateRepeat() {
+ String sequence = "123";
+
+ System.out.println( sequence + " repeated with Strings.repeat() ten times is: " + Strings.repeat( sequence, 10 ) );
+ }
+
+ public static void demonstratePadEnd() {
+ String message = "Alex went to sleep";
+ Character padChar = '.';
+
+ System.out.println( "Our string padded with Strings.padEnd() to 25 characters is: " + Strings.padEnd( message, 25, padChar ) );
+ }
+
+ public static void demonstratePadStart() {
+ String message = "Alex went to sleep";
+ Character padChar = '.';
+
+ System.out.println( "Our string padded with Strings.padStart() to 25 characters is: " + Strings.padStart( message, 25, padChar ) );
+ }
+
+}

0 comments on commit 4e5a6f7

Please sign in to comment.