Java strings

String in Java are objects. By defining Strings you have access to different standard methods to manipulate it.

You can create Strings in two ways. The common and most used way is the second one:

String str = new String (“I feel good”);
String str = “I feel good”;

strings

Strings are immutable

A string cannot be changed once it has been instantiated, for example:

String str = “Hi”;
str = “great”;

It seems that the first str is the same as the second one, but in fact they are two different Strings. Strings are immutable, because that way is faster than making it dynamic.

 

Methods of String

The String object has many methods which can be used to return information a bout the string or to change it. The following methods are very important for every programmers. That is why we pay more attention to them by giving some exercises below.

 

Method Description
length() Returns an int of the number of characters
toUpperCase () Returns a new String with with all uppercase letters
toLowerCase () Returns a new String with all lowercase letters
equals () Returns true if the Strings have the same length and same characters (case sensitive)
equalsIgnoreCase () Same as equals(), but not case sensitive
charAt () Retrun the character at the index
indexOf () Returns the index of the first or last occurrence of a character of substring
lastIndexOf ()
substring () Returns a substring from a String
trim() Returns a new String with leading and trailing white space removed

 

Examples:

String str = “Welcome To Megisoft.com!”;
String str2 = “Welcome To Megisoft.com!”;
String str3 = “Welcome to megisoft.com!” ;

  • str.equals(str2) returns “true”.
  • str.equals(str3) returns “false”. Case sensitive
  • str.equalsIgnoreCase(str3) returns “true”. They are the same. The method ignores case sensitivity.
  • str.length() returns “24”.
  • str.toUpperCase() returns “WELCOME TO MEGISOFT.COM!”.
  • str.toLowerCase() returns “welcome to megisoft.com!”.
  • str.charAt(8) returns “T”.
  • str.indexOf(‘m’) returns “5”. indexOf starts with 0.
  • str.lastIndexOf(‘e’) returns “12”. The index of the last ‘e’ is 12.
  • str.substring(11) returns “Megisoft.com!”.

If you want to return only “Megisoft from the string, then you need to pass two parameters to the substring method

  • str.substring(11, 19) returns only “Megisoft” without “.com”.
  • The trim() method removes white spaces from the string
  • String str4 = ”     Megisoft     “;
  • str4.trim() returns “Megisoft”.

 

Exercise 1

Which one of the following options removes only the link spam “http://www.xyz51.com” from the comment?

String str = “Please visit my website http://www.xyz51.com”

Select the correct answer.


 

 

Exercise 2 : Java String Objects

What happens when you try to compile and run the following program?

class MyString {

	public static void main(String[] args){
		String str1 = "str";
		String str2 = new String("str");
		if(str1 == str2) {
			System.out.print("-X");
		}
		if(str1.equals(str2)) {
			System.out.print("-Y");
		}
		else {
			System.out.print("-Z");
		}
	}
}

Select the correct answer.


Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.


Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

4 Comments

Add a Comment
  1. Your content is great, I am addicted to your quizzes 🙂

  2. Awesome website Sar, I love your method!

  3. Hi Sar! Can I find the answers of the DZone quizzes here?

    1. All the quizzes that I publish on DZone are exclusively designed for them. You can find all the answers if you visit my page on DZone.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017