Useful String Methods

     

Useful String Methods

In keeping with the habit I'm trying to develop, of focusing on code that you can run and change and mess around with, and get you in the habit of doing instead of reading, let's just plunk down some code that shows off some of the useful methods of the java.lang.String class.

StringDemo.java

 

 package net.javagarage.demo.String; /** <p>  * Demonstrates how to use Strings to your advantage.  * </p>  * @author HewittE  * @since 1.2  * @version 1.0  * @see  **/ public class StringDemo { private static void print(String s){ System.out.println(s); } private static void print(char c){ System.out.println(c); } private static void print(int i){ System.out.println(i); } private static void print(boolean b){ System.out.println(b); } public static void main(String[] args) { StringDemo sd = new StringDemo(); //make a String to work with String s1 = "Java Rockstar"; //return the character at a given position print(s1.charAt(2)); //prints "v" String s2 = "zzzzz"; print(s1.concat(s2)); //prints "Java Rockstarzzzzz" print(s2.concat(s1)); //prints zzzzzJava Rockstar print(s1.endsWith("r")); //prints "true" print(s2.endsWith("y")); //prints "false" print(s1.equals("Java Rockstar")); //prints "true" print(s1.equalsIgnoreCase("java rockstar")); //prints "true" print(s1.equals(s2)); //prints "false" print("muscle car".equals("cool")); //prints "false" //prints java.lang.String System.out.println(s2.getClass().getName()); //does string match this regular expression? //this regex checks if String is any digit print("7".matches("[0-9]")); //prints "true" print("whoa!".matches("[0-9]")); //prints "false" print(s2.length()); //prints "5" double d = 21D; //this is a static method, so we use the class name //(String) and then call the method on the class System.out.println(String.valueOf(d)); //prints "21.0" //you can do this with floats, ints, chars, etc. char[] chars = {'m','a','m','b','o'}; String charString = String.valueOf(chars); print(charString); //prints "mambo" //removes trailing and leading whitespace s1 = " s p a m "; print(s1); //prints "s p a m" //check the pool print(s2.intern()); } } 

The weirdo here is definitely intern() . That method checks the String pool and sees if there is already a String containing a meaningfully equivalent String, and if so, returns it. If not, the String is added to the pool and a new reference is returned.

Here is the output of running the preceding code:

 

 v Java Rockstarzzzzz zzzzzJava Rockstar true false true true false false java.lang.String true false 5 21.0 mambo s p a m zzzzz 

So to recap, the JVM maintains a pool of unique Strings, which is empty when the JVM starts. As you create Strings, they are added to the pool. All Strings, including constant expressions with a String value, are interned. According to the Java 5.0 documentation, "for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true." You don't typically need to call intern yourself.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net