Introduction to Java using NetBeans

Section 2.2 : String Data


Learning Goals

We are Learning .......

- string methods

Success Criteria

What I'm Looking For......

- bug free java applications that efficiently solve the problems presented
- problem solving skills (try before you ask)
- efficient algorithm implementation
- formated and documented code
- organized website with code and executables

Curriculum Expectations

Programming Concepts and Skills
- demonstrate the ability to use different data types and expressions
- use proper code maintenance techniques when creating computer programs.

Software Development
- manage the software development process effectively



What is a String

As we saw in the previous section a String is any group of characters. When we set a string value we enclose the characters 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.
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

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"

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

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

" There are 27 characters in your fullname"

" There are 9 characters in your firstname"

" There are10 characters in your middlename"

" There are 8 characters in your lastname"

3. Display your initials. "FML"

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

 

ICS4U

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

 

ICS4C

4. Display the entire name in Upper Case Letters

5. Display the entire name in Lower Case Letters