Java String

A String is used to store lines of text.

public class MyString {


  public static void main(String[] args) {

    String s = "Greetings all!";
	
    System.out.println(s);
	
  }
}

The output...

Greetings all!

But in Java, Strings are also objects. As such, they come with plenty of built in methods. Let's take a look at some of them.

charAt() -gets the chacter at a given position.

Example:

String s = "Greetings all!";
	
System.out.println(s.charAt(0));

Output:

G

compareTo() -compares the first string to another and returns a value of 0 if both Strings are equal. It returns a negative number if the first String would come before the second String alphabetically and a positive value if the second String would come first.

Example:

String s1 = "bat";
String s2 = "cat";
	
System.out.println(s1.compareTo(s2));

Output:

-1

compareToIgnoreCase() -same as above but not case sensitive.

concat() -this joins a second String to the end of the first.

Example:

String s1 = "bat";
String s = "Greetings all!";
	
System.out.println(s.concat(" Goodbye..."));

Output:

Greetings all! Goodbye...

contains() - returns true if the first String contains the second.

Example:

String s = "Greetings all!";
	
System.out.println(s.contains("all"));

Output:

true

contentEquals() - returns true if the String value is the same as the supplied value.

Example:

String s = "Greetings all!";
	
System.out.println(s.contentEquals("all"));

Output:

false

copyValueOf() - copies the value of an array to a String.

Example:

char[] myArray = {'H','i',' ','t','o',' ','a','l','l','.'};
	
String s = "";
s = s.copyValueOf(myArray);
	
System.out.println(s);

Output:

Hi to all.

startsWith() - returns true if the String starts with the given value.

Example:

String s = "Greetings all!";
	
System.out.println(s.startsWith("Gr"));

Output:

true

endsWith() - returns true if the String ends with the given value.

Example:

String s = "Greetings all!";
	
System.out.println(s.endsWith("all."));

Output:

false

equals() - returns true if two Strings have the same value. It will also return false if the supplied value is not a String object, like a character array.

Example:

String s1 = "Hi";
char[] s2 = {'H','i'};
	
System.out.println(s1.equals(s2));

Output:

false

equalsIgnoreCase() - same as above, not case sensitive.

Example:

String s1 = "Hi";
String s2 = "hi";
	
System.out.println(s1.equalsIgnoreCase(s2));    

Output:

true

indexOf() - returns the position in the String of the first found occurance of given value. Returns -1 if not found.

Example:

String s = "Greetings all!";
	
System.out.println(s.indexOf("all"));

Output:

10

lastIndexOf() - returns the position in the String of the last found occurance of given value. Returns -1 if not found.

Example:

String s = "hi hi hi";
	
System.out.println(s.indexOf("hi"));

Output:

6

isEmpty() - returns true if the String is empty.

Example:

String s = "Greetings all!";
	
System.out.println(s.isEmpty());

Output:

false

length() - returns the number of characters in the String.

Example:

String s = "Greetings all!";
	
System.out.println(s.length());

Output:

14

replace() - returns a String with the replaced value. The first argument is what to replace. The second is the new value to insert. If the first argument is not found, no replacement is made.

Example:

String s = "Greetings all!";
	
System.out.println(s.replace("all", "everyone"));

Output:

Greetings everyone!

substring() - returns a portion of the String. The first argument is start position. The second is the end position.

Example:

String s = "Greetings all!";
	
System.out.println(s.substring(0, 9));

Output:

Greetings

toCharArray() - returns a character array with the same value as the String.

Example:

String s = "Greetings all!";
	
char[] s2 = s.toCharArray();
	
System.out.println(s2);

Output:

Greetings all!

toLowerCase() - returns a String with the same value, but all lowercase letters.

Example:

String s = "Greetings all!";
	
System.out.println(s.toLowerCase());

Output:

greetings all!

toUpperCase() - returns a String with the same value, but all uppercase letters.

Example:

String s = "Greetings all!";
	
System.out.println(s.toUpperCase());

Output:

GREETINGS ALL!

trim() - removes any blank space at the start and end of the String.

Example:

String s = "   Greetings all!   ";
	
System.out.println(s.trim());

Output:

Greetings all!


I hope you found this tutorial helpful. Thanks for reading.

Next Topic: Random Numbers

Comments

Popular Posts