You are on page 1of 1

Java Strings - Quick Reference Guide

Creating a String: String s = new String ( "Hello World"); or String s = "Hello World"; Java String Methods ( all methods listed are public ) These methods use zero-based indexing

Strings in Java are immutable ( cannot be changed ). "Changing" a String causes a new String object to be created.
Requires an integer argument which indicates the position within the String of the character to be returned. ( start counting at 0 ). pos is in the range 0 to length of the String 1.

char charAt(int pos)

s1.charAt(3)

[ This would return the character in the 4th position ]

int compareTo(String s)

Returns: Zero if the strings are the same Negative number if the calling object is less than the argument object Positive number if the calling object is greater than the argument object

s1.compareTo(s2) [ s1 is the calling object, s2 is the argument object ]


boolean endsWith(String s) boolean equals(String s) boolean equalsIgnoreCase(String s) String [ static method ] format(String format, Object... args) int indexOf(char ch) int length( ) Takes a String argument and returns true or false if a String object does or does not end with the specified argument.

s1.endsWith("world")
Evaluate contents of two Strings to determine if they are equivalent.

s1.equals("Hello")

[ This does a case sensitive comparison ]

Ignores case when determining if two Strings are equivalent.

s1.equalsIgnoreCase("Hello") [ Comparison is not case sensitive ]


Returns a formatted string using the specified format string and arguments. Arguments are the same as when using System.out.printf( ) method

s1 = String.format( "Your total is $%9.2f\n", total );


Determines whether a specific character occurs within a String. Returns the element number if found, else 1. ( start counting at 0 )

s1.indexOf('e')
Returns the length of the String ( start counting at 1 )

s1.length( )

s = "abc";

s.length() --> 3

String[ ] split(String regex)

Splits this string around matches of the given regular expression. Similar to use of StringTokenizer object. Returns an array of Strings (tokens).

String sentence, words[]; sentence = input.nextLine(); words = sentence.split(" "); //" " is the reg. exp.
Takes a String argument and returns true or false if a String object does or does not begin with the specified argument.

boolean startsWith(String s) String substring(int n, int m) String toLowerCase( ) String toUpperCase( ) String [ static method ] toString( <type> x) String trim( )

s1.startsWith("world")
Extracts a substring of a String begining at element n and ending at element m-1

s1.substring(n, m)

s="Yes and No"; s.substring(4,7) --> "and"

Converts a String to its lowercase equivalent.

s1.toLowerCase( )

s = "Good"; s.toLowerCase() --> "good"

Converts a String to its uppercase equivalent.

s1.toUpperCase( )

s = "Good"; s.toUpperCase() --> "GOOD"

Converts any primitive type to a String.

String.toString(x)

int x=123; s = String.toString(x)--> "123"

Returns a copy of the string, with leading and trailing whitespace omitted.

s1.trim()

s = " Hello World "; s = s.trim() --> "Hello world"

You might also like