Introduction to Java using NetBeans

Section 2 - Using String Data



As we saw in the previous section a String is any group of characters that is enclosed in quotes.

Example Icon Example

  • “Hello World”
  • “123456”

Every character in a string has an index position in the string (an integer). The index position of the first character is 0.

Example Icon Example


  • the character y is in position 0
  • the character w is in position 5



String Methods


Method Syntax Description
charAt myString.charAt(pos) Returns the character at the position (pos).
charCodeAt String.charCodeAt(pos) Returns the character code of the character at the position (pos).
indexOf myString.indexOf
Returns the location of the inner string in the main string.
replace myString.replace(regexp,replacement) Replaces the pattern within the string.
slice myString.slice(start,end) Returns the substring starting at position (start) and ending at position (end).
String String(notAString) Converts a number or other value to a string.
substring myString.substring(start,end) Returns the substring starting at position (start) and ending at position (end)
toLowerCase myString.toLowerCase() Returns the string with lower case letters.
toUpperCase myString.toUpperCase() Returns the string with upper case letters.
concat myString.concat(otherString) Returns a new string with the second string appended to end of the first string.
replaceAll myString.replaceAll("(?i)"+stringsearch,stringreplace) Replaces without considering the Case of the string (?i = case insensitive) Can also be used with replaceFirst
length myString.length Returns the number of characters in the string.



Example Icon Example

Below are lines of code that make use of string methods. Try to figure out what each line of code will print and then check your answers by typing up the code.

Program Code

Questions Icon Check Your Understanding

  1. What output will each of the statements shown below produce?




Assignment 2.6 String Manipulation

Create an application that asks the user for their first, middle and last names and stores them in a single string

What is your Name?
Firstname Middlename Lastname

Display the name using the formats:

1. "Your Firstname is"
"Your Middlename is"

"Your Lastname is"

1. "Your name in reverse order is Lastname, Firstname " in an appropriate message box

HINT: Find the position of the spaces and then use substring

2. Display the number of characters in the name, firstname, and lastname, not including spaces.

" There are 17 characters in your fullname"

" There are 9 characters in your firstname"

" There are 8 characters in your lastname"

3. Display your initials. "FML"

Remember that the program must work no matter what name is entered.



4. Regardless of how the name is entered (upper, lower, combination) display the names with upper case first letters and lower case everything else.



4. Display the entire name in Upper Case Letters

5. Display the entire name in Lower Case Letters


Posting your work

1. Upload the Java Projects to your Gravelle Folder (Share with me only) and put a link for each assignment on your website.

JAR files which are executable java files do not work on projects without a display so we will not use them in this assignment.