You are on page 1of 6

Programming NC IV

1
Strings

Strings

This module covers the fundamentals of understanding, using and


manipulating strings within the Java environment.
Upon completing this module, the students should be able to understand and
do the following:
1. understand how strings work;
2. use string methods properly; and
3. manipulate strings and substrings.

How Do Strings Work and What are They For?


What is a string?
A string is used both as a data type or the object itself that co ntain a sequence
of characters. The data type and object are commonly used in tandem, such
as declarations but can also be used separately.
In declaring a string, follow this syntax:
String <String name> = “<value>”;
Examples:
String greeting = “Hello Java!”;
String fullName = “Archer B. Chapwick”;

You’ll notice that compared to char, String uses double quotes (“”).
Let’s use that example in a simple printing program:

Figure 1. String Declaration

Course Module
Manipulating Strings
The string is very versatile or flexible that it can be readily converted to other
data types or manipulate to what you and/or your application needs.

Conversion
Some methods built in to the string object can be used to convert it to int,
float, double, byte and so on. Other data types can also be converted to a
string, but that is a bit trickier than the other way around.

Here are a few examples:


In Figure 2: Line 7, we declare a string data type followed by a variable, then
assign a sequence of characters to a string variable named “str”.

Figure 2. String to Int

Lines 7&8 are two methods of converting an appropriate string to integer(s).


int declares the data type for the variable “x” and assigned to the first method
“Integer.valueOf(str)” returns a new instance of
“java.lang.Integer” while “Integer.parseInt(str)” returns a
primitive “Int”.
Most of the other conversion types like “Float.valueOf(str)” are the
same as the said methods, but there are more to string conversions than
these.
Programming NC IV
3
Strings

Manipulation
The versatility of the string grants the user several abilities – including, but
not limited to, the ability to slice, append, add, replace, remove, count the
contents and even get the position of a certain character within the sequence.

Figure 3. String Manipulation

In Figure 3: Line 6 you can see that we added some letters to the string,
resulting to “123abc” so that the manipulation of the string can be well seen.
Figure 3 Set 1. In line 8 you can see that we called for the string method
“length( )”. Remember that strings are like arrays in one way or
another, the index starts at “0” so a string ending at the 5 th container in the
array still counts six characters including the “0”.

Course Module
Figure 4. Updated String

This method returns the character count within the string; returns “0” if the
string is empty and in this case “6” as seen in the second line of the output
window on the bottom part of the image.
As you have noticed in Figures 2 & 3, some of the print expressions
“Sytem.out.println( )” has a “+” operator in them. This is called
concatenation.
The example in Sets 1 & 2 Figure 3 is an example of concatenating a string
with an integer, but strings can also be concatenated to floats, doubles, bytes
and other strings.
Ex.
String str_1 = “foo”;
String str_2 = “bar”;
Sytem.out.println(foo+bar);

or
String a = “123”;
String b = “abc”;
String c = a + b;
Sytem.out.println(c);

Figure 5. Concatenation
Programming NC IV
5
Strings

The first example will yield a result of “foobar” and the second one will be
“123abc”. These examples are not the only way to do it, you can always try
experimenting and concatenating different data types so you know which
works on what and what doesn’t.
In Set 3 Figure 3 we see how a substring works. The basic function of a
substring is to slice at an arbitrary point in the character sequence and
return the value.

Figure 6. Substring

The example in Figure 6 makes use of two values for the method parameters,
the first value indicates the first character and the second value tells the
substring to stop before that index.
After the substring determines where to start and when to stop, the
method returns the characters from the start index and before the end
index as shown in Figure 6.

Course Module
Other useful methods of the string are:
1. trim( )
2. toLowerCase( )
3. replace(char old,char new)
4. charAt( )
5. compareTo( )
6. indexOf( )

trim( ) – removes white spaces or any trailing null characters at the end of a
string.
toLowerCase( )/toUpperCase( ) – changes the string to all caps or all lower
cased.
replace(char old, char new) – replaces all occurrences of an old character
with the new one as specified.
charAt( ) – returns the character at the specified index.
compareTo( ) – compares a string to another string and returns 0 if the
strings are equal, 1 or any positive integer greater than 0 if the first string is
greater and -1 or any value less than zero if the first string is less than the
second.
indexOf( ) – returns the index of the first occurrence of a character.

These methods are what you would mostly see being used in common
programming practices. But always keep in mind that everything has a work-
around or other overloaded methods that are not always displayed unless
looked for.

References

Oracle. Java™ Platform Standard Ed. 7. Retrieved from:


https://docs.oracle.com/javase/tutorial/java/

You might also like