Methods of the String Class (Part 2)

This is the continuation of the previous part of Methods of the String Class (Part 1) . This the part 2 of that tutorial. Hope you enjoyed it and let’s get started.

The substring method

  • returns new strings that are sub strings (parts) of the referred string (original String) object.
  • this method requires two parameters.

    originalString.substring(startingIndex, endIndex)

  • the end index is not a must. If not stated, the end will the end of index of the original string.substring
  • as you can see even the space has given the index. (2nd sub string)
  • let’s further explore this method. What will happen if you give the end index, a higher values than the length of the original string ([length – 1] to be exact) or what will happen if you give a negative value?substring error

 

 

 

 

 

 

 

  • as you can see, Java is too smart for us. Java has identified it as an exception.
  • giving an negative value will also end up with the same exception. (the exceptions can be handled using try-catch blocks which will be discussed later).

The trim method

  • returns the whole original string after removing the leading and trailing white spaces.

    originalString.trim()

    tri

  • check the difference between original text and resulting text.

The toLowerCase Method and toUpperCase Method

  • returns the whole string in lower case letters and upper case letters respectively.upper lower

The equalsIgnoreCase Method

  • returns a boolean after comparing the original text with the text given in the parameter.

    originalString.equalsIgnoreCase(“comparing string”)

    comparingString.equalsIgnoreCase(“originalString”)

    equalIgnoreCase

This is it for the methods of the string class but I will come back with a another set of methods which feat the java string buffer class.

So until that Happy Hacking.

 

 

 

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s