0% found this document useful (0 votes)
14 views7 pages

Java String Methods Overview

This document is a lesson plan on Java String methods for an Object-Oriented Programming course at Pampanga State University. It covers various string operations including storage, ASCII values, case conversion, comparison, concatenation, and methods for manipulating strings. Additionally, it includes review questions and answers to reinforce understanding of the concepts presented.

Uploaded by

ronnelbalcita23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Java String Methods Overview

This document is a lesson plan on Java String methods for an Object-Oriented Programming course at Pampanga State University. It covers various string operations including storage, ASCII values, case conversion, comparison, concatenation, and methods for manipulating strings. Additionally, it includes review questions and answers to reinforce understanding of the concepts presented.

Uploaded by

ronnelbalcita23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State

University) Sto. Tomas Campus

Lesson 7: Java String Methods

Course Code: ITELEC 1


Course Title: Object-Oriented Programming (Java)
Prepared by: Alvin Herrera, Instructor 1

Lesson Content:
1. Java String
2. How Java stores strings?
3. ASCII
4. How to get Decimal Value of a Character in ASCII?
5. Converting to Upper and Lower Case
6. Comparing Strings
7. Concatenate Strings
8. Retrieving Character Index
9. Ends with… Starts with
10. Substring
11. Equals Method
12. charAt Method
13. Replace Method
14. Trim Method
15. Formatted Strings

1. Java String
There's more to strings than meets the eye. Unlike int
variables, or double variables, strings are objects. What this
means in practice is that you can do things with strings of text
that you can't do with int or double variables. (The same applies
to the primitive data types boolean, byte, single, char, float,
long and short: they are not objects like strings are.) Before we
get to manipulating strings of text, here's some basic information
on what strings actually are.

2. How Java stores strings?


A string is a series of characters held under a variable name.
Take the following string:

String someText = "Bill";

This tells Java to set up a string object with the four characters
"B", "i", "l" and another "l".

3. ASCII
ASCII (American Standard Code for Information Interchange) is
a standard character encoding used in telecommunication. It
currently defines 95 printable characters including 26 upper case
letters (A to Z), 26 lower case letters, 10 numerals (0 to 9),
and 33 special characters including mathematical symbols,
punctuation marks, and space characters. They represent text in,

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines
PAMPANGA STATE UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

telecommunications equipment, and devices. These include numbers,


upper and lowercase English letters, functions, punctuation
symbols, and some other symbols.

4. How to get Decimal Value of a Character in ASCII?


Source Code Output

public class Main { 65


/
public static void
main(String[] args) {

// Character to Decimal
char chrChar1 = 'A';
int asciiVal = chrChar1;
[Link](asciiVal);

// Decimal to Character char


chrChar2 = (char)(47);
[Link](chrChar2);

}
}

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State
University) Sto. Tomas Campus

5. Converting to Upper and Lower Case


Converting your Java strings of text to upper or lower case
is fairly straightforward: just use the inbuilt methods
toUpperCase and toLowerCase.

To use a string method you first type the string you want to
work on. Type a dot after the variable name and you'll see a list
of available methods that you can use on your string. Select
toUpperCase or toLowerCase. (The method needs the empty round
brackets after it.)

String strChangeCase = “Change Case”;


[Link]. println([Link]());
[Link]. println([Link]());

6. Comparing Strings
You can compare one string to another. You can use an inbuilt
string method called compareTo.

int rs;
String w1 = "App", w2 = "Ape";
rs = [Link](w2);
if(rs == 0)
[Link](w1 + " and " + w2 + " are same
word"); else
[Link](w1 + " and " + w2 + " are not same word");

7. Concatenate Strings
The concat method is used to join strings together. Here's a
simple example:

String strFirstWord = “Ja”;


String strAmmend = “va”;
[Link]. println([Link](strAmmend));

// Java

8. Retrieving Character Index


The indexOf method is used to locate a character or string
within another string. For example, you can use it to see if there
is a @ character in an email address.

String strEmailAddress = "joyboy@[Link]";


int intPos = [Link]('@');
[Link]. println(intPos);
String strDotCom = ".com";
[Link]. println([Link](strDotCom, intPos));

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State
University) Sto. Tomas Campus

9. Ends with… Starts with


You need to set up a Boolean variable for endsWith, because
the method returns an answer of true or false. The string you're
trying to test goes between the round brackets of endsWith, and
the text you're searching goes before it. If the text is in the
search string then a value of true is returned, else it will be
false. You can add an if … else statement to check the value:

String strEmailAddress = "joyboy@[Link]";

[Link].
println([Link]("joy"));
[Link]. println([Link](".com"));

10. Substring
One really useful method available to you is called substring.
This method allows you to grab one chunk of text from another.

For example, we want to modify the name “Bill Gates” to “Gall


Bites”, we can use substring method.

// String variable with initial value Bill Gates


String strName = "Bill Gates";

// String variables that will hold chunk of text later


String strFirstnameChars = "", strOtherFirstnameChars = "",
strSurnameChars = "", strOtherSurnameChars = "";

// Grab Bi from Bill using substring method and store it to


strFirstnameChars
strFirstnameChars = [Link](0, 2);

// Get the index of the white space


int intSpacePos = [Link](" ");

// Grab Ga from Gates using substring method and store it to


strSurnameChars
strSurnameChars = [Link](intSpacePos + 1, (intSpacePos
+ 1) + 2);

// Grab the remaining characters ll from Bill and store them in


strOtherFirstnameChars
strOtherFirstnameChars = [Link](2, intSpacePos);

// Grab the remaining characters tes from Gates and store them in
strOtherSurnameChars
strOtherSurnameChars = [Link]((intSpacePos + 1) + 2);

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State
University) Sto. Tomas Campus

// Concatenate the chunk of text to form a new name Gall Bites and
Display to the user
[Link]([Link](strOtherFirstnameChars
) .concat("
").concat(strFirstnameChars).concat(strOtherSurnameChars));

11. Equals Method


You can check two integer values to see if they are the same using
the equals method. In between the round brackets of the equals
method, you place the integer you're trying to check. The other
integer goes before the equals method. Java will then tell you
(true or false) whether the two are the same.

// Compare Integer
int intNum1 = 5, intNum2 = 5;
Integer integerNum1 = new Integer(intNum1), integerNum2 = new
Integer(intNum2);
boolean isMatch = [Link](integerNum2);
[Link](isMatch);

12. charAt Method


You can check to see which single character is in a particular
string. The charAt method is used for this in Java.

String strEmaildAddress = "joyboy@[Link]";


char chCharAt = [Link]( 6 );
[Link](chCharAt);
// @

13. Replace Method


The replace method in Java is used to replace all occurrence of a
character/s in a particular string. It works with Strings and
Chars.

String strEmaildAddress = "joyboy@[Link]";


String strNewEmailAddress = [Link]("joy",
"sad");
[Link](strNewEmailAddress);
// sadboy@[Link]

14. Trim Method


You can trim white space from your strings White space is things
like space characters, tabs, and newline characters - the
characters you can't see, in other words.

String amend = " white space ";


amend = [Link]( );

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State
University) Sto. Tomas Campus

So the trim method goes after the string you want to amend. The
blank characters before the word "white" and after "space" in the
code above will then be deleted.
Optionally, you can use the replace method to remove the white
space between white and space.

15. Formatted Strings


Strings of text can be formatted and output with the printf
command. The printf command understands a series of characters
known as a format specification. It then takes a string of text
and formats it, based on the format specification passed over. As
an example, supposed we wanted the Output window to display text
in neat columns, like this:

The first

column is left-justified, and the second column is right


justified. The code for the Exam_Name and Exam_Grade headings was
this:

String heading1 = "Exam_Name";


String heading2 = "Exam_Grade";
[Link]( "%-15s %15s %n", heading1, heading2);

To get the left-justified column, you need a percentage symbol, a


minus symbol, the number of characters, and then the letter "s"
(lowercase). So ''%-15s'' means fifteen characters left-justified.
To get a right-justified column the same sequence of characters
are used, except for the minus sign.
To get a newline %n is used. Note that the characters are
surrounded with double quotes.

After a comma, you type the text that you want formatting. The
first comma in the code above separates the format specification
from the text being formatted.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE UNIVERSITY


(former Don Honorio Ventura State University)

Sto. Tomas Campus


Review Questions and their answers

1. What makes a Java String different from primitive data types like
int or double?
Ans. A String is an object, not a primitive type, which allows it to
use many built-in methods for text manipulation.
2. How does Java store a string such as String name = "Bill";? Ans.
Java stores the string as a sequence of characters ('B', 'i', 'l',
'l') inside a String object.
3. What is ASCII and what does it represent?
Ans. ASCII is a character encoding system that assigns decimal values
to letters, digits, and symbols.
4. How can you get the ASCII value of a character in Java? Ans. You can
assign a character to an int variable, and Java automatically
converts it to its ASCII value.
5. Which methods are used to convert a string to uppercase or
lowercase? Ans. The methods toUpperCase() and toLowerCase() are used
to convert strings to upper or lower case.
6. How does the compareTo() method work for strings?
Ans. compareTo() compares two strings and returns 0 if they are
equal, or a positive/negative number if not.
7. Which method is used to join two strings together?
Ans. The concat() method is used to combine two strings into one.
8. What does the indexOf() method do?
Ans. indexOf() searches for a character or substring in another
string and returns its starting index, or -1 if not found.
9. What is the purpose of the substring() method?
Ans. substring() extracts a portion of a string by specifying
starting and ending index positions.
10. How does the trim() method change a string?
Ans. trim() removes whitespace from the beginning and end of a string.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]

You might also like