Chapter 2.3.2 – Operations on Strings | Introduction to Programming Using Java

Chapter 2.3.2 – Operations on Strings | Introduction to Programming Using Java

 

2.3.2 Operations on Strings

 

A value of type String is an object. That object contains data, namely the sequence of characters that make up the string. It also contains subroutines. All of these subroutines are in fact functions. For example, every string object contains a function named length that computes the number of characters in that string. Suppose that advice is a variable that refers to a String. For example, advice might have been declared and assigned a value as follows:

String advice; advice =

“Seize the day!”;

Then advice.length() is a function call that returns the number of characters in the string “Seize the day!”. In this case, the return value would be 14. In general, for any string variable str, the value of str.length() is an int equal to the number of characters in the string that is the value of str. Note that this function has no parameter; the particular string whose length is being computed is the value of str. The length subroutine is defined by the class String, and it can be used with any value of type String.

 

Operations on Strings

 

It can even be used with String literals, which are, after all, just constant values of type String. For example, you could have a program count the characters in “Hello World” for you by saying

 

Chapter 2.3.2 - Operations on Strings | Introduction to Programming Using Java

 

The String class defines a lot of functions. Here are some that you might find useful. Assume that s1 and s2 refer to values of type String:

  • equals(s2) is a function that returns a boolean value. It returns true if s1 consists of exactly the same sequence of characters as s2, and returns false otherwise.
  • equalsIgnoreCase(s2) is another boolean-valued function that checks whether s1 is the same string as s2, but this function considers upper and lower case letters to be equivalent. Thus, if s1 is “cat”, then s1.equals(“Cat”) is false, while s1.equalsIgnoreCase(“Cat”) is true.
  • length(), as mentioned above, is an integer-valued function that gives the number of characters in s1.
  • charAt(N), where N is an integer, returns a value of type char. It returns the Nth character in the string. Positions are numbered starting with 0, so s1.charAt(0) is actually the first character, s1.charAt(1) is the second, and so on. The final position is s1.length() – 1. For example, the value of “cat”.charAt(1) is ’a’. An error occurs if the value of the parameter is less than zero or greater than s1.length() – 1.
  • substring(N,M), where N and M are integers, returns a value of type String. The returned value consists of the characters in s1 in positions N, N+1,…, M-1. Note that the character in position M is not included. The returned value is called a substring of s1.
  • indexOf(s2) returns an integer. If s2 occurs as a substring of s1, then the returned value is the starting position of that substring. Otherwise, the returned value is -1. You can also use s1.indexOf(ch) to search for a particular character, ch, in s1. To find the first occurrence of x at or after position N, you can use s1.indexOf(x,N).
  • compareTo(s2) is an integer-valued function that compares the two strings. If the strings are equal, the value returned is zero. If s1 is less than s2, the value returned is a number less than zero, and if s1 is greater than s2, the value returned is some number greater than zero. (If both of the strings consist entirely of lower case letters, then “less than” and “greater than” refer to alphabetical order. Otherwise, the ordering is more complicated.)
  • toUpperCase()is a String-valued function that returns a new string that is equal to s1, except that any lower case letters in s1 have been converted to upper case. For example, “Cat”.toUpperCase() is the string “CAT”. There is also a function s1.toLowerCase().
  • trim() is a String-valued function that returns a new string that is equal to s1 except that any non-printing characters such as spaces and tabs have been trimmed from the beginning and from the end of the string. Thus, if s1 has the value “fred “, then s1.trim() is the string “fred”.

For the functions s1.toUpperCase(), s1.toLowerCase(), and s1.trim(), note that the value of s1 is not modified. Instead a new string is created and returned as the value of the function. The returned value could be used, for example, in an assignment statement such as “smallLetters = s1.toLowerCase();”. To change the value of s1, you could use an assignment “s1 = s1.toLowerCase();”.

Here is another extremely useful fact about strings: You can use the plus operator, +, to concatenate two strings. The concatenation of two strings is a new string consisting of all the characters of the first string followed by all the characters of the second string. For example, “Hello” + “World” evaluates to “HelloWorld”. (Gotta watch those spaces, of course—if you want a space in the concatenated string, it has to be somewhere in the input data, as in “Hello ” + “World”.)

 

Chapter 2.3.2 - Operations on Strings | Introduction to Programming Using Java

 

Let’s suppose that name is a variable of type String and that it already refers to the name of the person using the program. Then, the program could greet the user by executing the statement:

System.out.println(“Hello, ” + name + “. Pleased to meet you!”);

Even more surprising is that you can actually concatenate values of any type onto a String using the + operator. The value is converted to a string, just as it would be if you printed it to the standard output, and then it is concatenated onto the string. For example, the expression “Number” + 42 evaluates to the string “Number42”. And the statements

 

Chapter 2.3.2 - Operations on Strings | Introduction to Programming Using Java

 

 

 

 

 

Obviously, this is very convenient. It would have shortened some of the examples presented earlier in this chapter.

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.2.1 – Variables | Introduction to Programming Using Java

Chapter 2.2.2 – Types and Literals | Introduction to Programming Using Java

Chapter 2.2.3 – Variables in Programs | Introduction to Programming Using Java

Chapter 2.3.1 – Built-in Subroutines and Functions | Introduction to Programming Using Java

1 thought on “Chapter 2.3.2 – Operations on Strings | Introduction to Programming Using Java”

Leave a Comment