A String is an Object that contains "functionalities" that allow you to do certain operations. These functionalities are called methods.
Methods are functionalities that belong to objects, such as String, which we will discuss more in detail on Ch. 4. Some of the popular methods can be seen in Table 3. Let us demonstrate these functionalities by testing them into the interactions window:
The length() method retrieves the total number of characters in the String. Let us define a String called s1, that will hold the String "hello", after calling the method, we obtain 5, since the String s1 has 5 characters, i.e., 'h', 'e', 'l', 'l', and 'o'.
> String s1 = "hello"
> s1
"hello"
> s1.length()
5
Java is a zero-based numbering language, meaning that we start counting from 0 and not from 1. Let us access each character from the String `hello` starting from index 0
> s1.charAt(0)
'h'
> s1.charAt(1)
'e'
> s1.charAt(2)
'l'
> s1.charAt(3)
'l'
> s1.charAt(4)
'o'
Although the String `hello` has a length of 5, a typical error into access the index representing the length of the String, in this case, is 5. This mistake will lead us to an exception (discussed on detail on Ch. Ch. 8) as follows:
> s1.charAt(5)
java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(Unknown Source)
Let us now redefine the variable s1, by providing a different value for the String.
> s1 = "hElLo gUyS hOw aRe yOu"
"hElLo gUyS hOw aRe yOu"
Notice that the variable s1 has a combination of the upper and lower case. The String counts with methods that change the case for the string. For example:
> s1.toUpperCase()
"HELLO GUYS HOW ARE YOU"
changes the String into upper case, where the toLowerCase() changes to a lower case.
> s1.toLowerCase()
"hello guys how are you"
It is important to recognize that if we call the variable s1, it will continue with the original state since we never update the variable.
> s1.toLowerCase()
> s1
"hElLo gUyS hOw aRe yOu"
Remember that the s1 is a variable, and the purpose of a variable is that its value eventually change. If that is the objective, then we need to assign (=) the new value of s1 to s1 as follows:
> s1 = s1.toLowerCase()
> s1
"hello guys how are you"
In addition to the functions (methods) listed above for the String, there is one additional called the substring that is quite useful for your programming projects.
The substring is a method from the object String that provides a partial subpart of the string depending on the two methods you use.
There are two methods for substring:
substring(pos): the substring from position 0 to position ‘pos’. E.g.,
If String s = “hello”, s.substring(3) will give you back the String: “lo”
substring(pos1, pos2): the substring from `pos1` to `pos2`. E.g.,
If String s = “hello”, s.substring(1,3) will give you back the String: “el”
In our previous example, we can demonstrate the two substring methods as follows:
> s1.substring(11)
"Lo gUyS hOw aRe yOu"
> s1.substring(6,11)
"gUyS h"
ACM CCECC
[Scope, Comments, and Programming Style]
SDF-18. Apply consistent documentation and program style standards.
[Keyboard Input and Dialog Boxes]
HCI-04. Write a simple application that uses a modern graphical user interface.
AP CS A
[Scope, Comments, and Programming Style]
MOD-2.C Describe the functionality and use of program code through comments.
MOD-2.C.1 Comments are ignored by the compiler and are not executed when the program is run.
MOD-2.C.2 Three types of comments in Java include /* */, which generates a block of comments, //, which generates a comment on one line, and /** */, which are Javadoc comments and are used to create API documentation.
CS1
[Scope, Comments, and Programming Style]
130.421.c.2.d Write programs with proper programming style to enhance the readability and functionality of the code by using meaningful descriptive identifiers, internal comments, white space, spacing, indentation, and a standardized program style;
130.421.c.6.s Demonstrate an understanding of the concept of scope;
[Keyboard Input and Dialog Boxes]
130.421.c.2.b Create interactive console display interfaces, with appropriate user prompts, to acquire data from a user;
Design, write, test, and debug a program that effectively uses the different structured data types provided in the language like strings, arrays/lists, dictionaries, sets
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)