Strings

6.3 Strings

STRINGS

The String is a Java class for representing a fixed collection of characters. You can represent a String in your code by using double quotes as follows:
     String greeting = "Hello World!";
There are some useful methods that you can use:
length()
returns the length of the string.
substring(int beginIndex,int endIndex)
returns a the substring starting from character position beginIndex and ending with position endIndex. Remeber that position 0 is the first character in the String
valueOf(anything)
returns a String that represents the argument. For example, it would convert the integer 64 to "64".

These aren't all the methods, but they will get you started. Also, there exists the concatination operator, +, and a function to print strings, System.out.println.


STRINGS EXAMPLES

  1.      String greeting = "Hello World!";
         System.out.println(greeting);
    
    outputs:
    Hello World!
  2.      String greeting = "Hello World!";
         System.out.println(greeting.substring(0,4));
    
    outputs:
    Hello
  3.      String greeting = "Hello World!";
         System.out.println(String.valueOf(greeting.length()));
    
    outputs:
    12
  4.      String greeting = "Hello World!";
         int greeting_length = greeting.length();
         System.out.println("The length of "+greeting+" is "+greeting_length);
    
    outputs:
    The length of Hello World! is 12
Next 6.4 Arrays

David Maxwell, who is still writing this, would like to hear your comments and suggestions.