Java Lab Documentation

String

Category:java.lang

The String class represents a sequence of characters. There exist a variety of methods within the String class for identifying specific characters within the string, for comparing strings, and searching for substrings.

Method Details

String

String(String original)

Creates a String object that represents the same sequence of characters as the argument

Parameters

NameTypeRequired?Description
originalString

a String that the String object will copy

Examples

String tempString = new String("hello");

compareTo

public int compareTo(String stringTwo)

The compareTo() method compares the String values and returns an int. The returned int is the number difference between two Strings. Both Strings are compared by the Unicode value of each character in the String.

  • If stringOne is greater than stringTwo, the method will return a positive number. "Greater than" means lexicographically greater than, so when stringOne comes after stringTwo.

  • If stringOne is less than stringTwo, the method will return a negative number. "Less than" means lexicographically less than, so when stringOne comes before stringTwo.

  • If stringOne equals stringTwo, the method will return 0.

The compareTo() method is case sensitive, meaning that capitalization does affect the calculation.

Parameters

NameTypeRequired?Description
stringTwoString

the String to be compared to the calling String object

Examples

String name1 = "James";
String name2 = "Bill";
String name3 = "James";

System.out.println(name1.compareTo(name2));   // prints 8 because James > Bill lexicographically 
System.out.println(name1.compareTo(name3));   // prints 0 because both are the same

equals

public boolean equals(String stringTwo)

The equals() method will compare the string that it is called on as well as the string passed as a parameter. The expression returns true if the two String contains the same sequence of characters, false otherwise.

Parameters

NameTypeRequired?Description
stringTwoString

the second String being compared to the calling String

Examples

String stringOne = "Apple";
String stringTwo = "Pear";
String stringThree = "Apple";

System.out.println(stringOne.equals(stringTwo)); 
// prints false

System.out.println(stringOne.equals(stringThree)); 
// prints true

indexOf

public int indexOf(String str)

The indexOf() method returns the index for the first occurrence of the specified character(s) in a String or -1 if the specified character(s) is not found in the String.

Parameters

NameTypeRequired?Description
strString

the String to locate within the initial String

Examples

String text = "Hello world, welcome to the universe.";
int result = text.indexOf("welcome");

System.out.println(result);

Output:

13

length

public int length()

The length() method returns the number of characters in the specified String.

Examples

String test = new String("Welcome");
System.out.println(test.length());

Output:

7

substring

public String substring(int start)

The substring() method returns a String containing the characters from the specified starting index to the end of the String.

Parameters

NameTypeRequired?Description
startint

the index of the first character to start at

Examples

Using substring()

String test = "welcome";
System.out.println(test.substring(2));

Output:

lcome

Overloads

substring

public String substring(int start, int end)

The substring() method returns a String containing the characters from the specified starting index up to (but not including) the specified ending index.

Parameters

NameTypeRequired?Description
startint

the index of the first character to start at

endint

the index of the last character to stop at

Examples

String test = "welcome";
System.out.println(test.substring(2,4));

Output:

lc

toString

public String toString()

The toString() method returns the String itself.

Examples

String test = new String("Welcome to our Home!");
System.out.println(test.toString());

Output:

Welcome to our Home!

toLowerCase

public String toLowerCase()

The toLowerCase() method returns the String converted to lowercase letters.

Examples

String test = "Welcome, Anna!";
System.out.println(test.toLowerCase());

Output:

welcome, anna!

toUpperCase

public String toUpperCase()

The toUpperCase() method returns the String converted to uppercase letters.

Examples

String test = new String("Welcome, Anna!");
System.out.println(test.toUpperCase());

Output:

WELCOME, ANNA!

compareToIgnoreCase

public int compareToIgnoreCase(String str)

The compareToIgnoreCase() method compares the String values, ignoring case differences, and returns an int.

  • If stringOne is greater than stringTwo, the method will return a positive number.
  • If stringOne is less than stringTwo, the method will return a negative number.
  • If stringOne equals stringTwo, the method will return 0.

The compareToIgnoreCase() method is not case sensitive.

Parameters

NameTypeRequired?Description
stringTwoString

the String to be compared to the calling String object

Examples

String name1 = "James";
String name2 = "Bill";
String name3 = "jAMES";

System.out.println(name1.compareToIgnoreCase(name2));   // prints 8 because James > Bill lexicographically 
System.out.println(name1.compareToIgnoreCase(name3));   // prints 0 because both are the same ignoring case

equalsIgnoreCase

public boolean equalsIgnoreCase(String stringTwo)

The equalsIgnoreCase() method will compare the string that it is called on as well as the string passed as a parameter. The expression returns true if the two String contains the same sequence of characters ignoring case, false otherwise.

Parameters

NameTypeRequired?Description
stringTwoString

the second String being compared to the calling String

Examples

String stringOne = "Apple";
String stringTwo = "Pear";
String stringThree = "aPPLE";
    
System.out.println(stringOne.equalsIgnoreCase(stringTwo)); 
// prints false
    
System.out.println(stringOne.equalsIgnoreCase(stringThree)); 
// prints true

trim

public String trim()

Returns a string with whitespace at the start and the end removed.

Examples

String test = "  welcome home   ";
System.out.println(test.trim());

Output:

welcome home