Introduction to Java using NetBeans

Section 2 - Using String Data

 

 

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

“yellow”

  • 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
(innerString,startPos)
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.
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.8 String Manipulation

Create an application that asks the user for their first name and their last name and stores them in a single string.

What is your Name?
Firstname Lastname

Display the name using the formats:

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

HINT: Find the position of the space 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"

ICS3U

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

5. Make everything work if First Middle Last names are entered.

ICS3C

3. Display the entire name in Upper Case Letters

4. 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.