Characters & Strings
Declaring Characters and
Strings
char is a primitive data type much like int, double, etc. It allows
us store single characters, such as letters in java
char myLetter;
myLetter = ‘ ’;
myLetter = ‘w’;
To initialize a character to a blank space with use single quotes.
Declaring Characters and
Strings
String is a class. It is used to store one or more characters.
String myWord;
myWord = “ ”;
myWord = “word”;
To initialize a String to a blank space with use double quotes.
Strings
• Since String is a class there are associated methods that we
can use.
• We have used the equals() method – this allows us compare
two strings and see if they are equal.
Length of String
• You can easily find the length of a string in Java.
• The method length() returns an integer value that can then be printed to
the screen, e.g
String name = “SUMATRA”;
int length = name.length;
The variable name will be set to equal Sumatra, and the variable length
will be set to be equal to the number of characters in the string Sumatra
which is 7
public class StringLength{
public static void main(String args[]){
String message =“Let’s count how many letters are in this string”;
int length = message.length();
System.out.println(“The length of the message is”+length);
}
}
Individual Characters in a
String
• To access the individual characters in a String we use the
charAt() method.
• We do this by using the charAt method.
String name = “Sumatra”;
int size = name.length ();
for (int i=0; i<size; i++) {
System.out.println(name.charAt(i));
}
Using the for loop we print out each character or the String
on a separate line
Individual String Characters
A string can be know as an array of letters.
Using charAt, we are accessing individual
elements
To refer to the first character in the string, we
say
name.charAt(0);
Individual characters are indexed from 0 up to
size-1
Strings and Loops
We need to be careful with our loops, if
size = name.length ();
then we can use either of the following:
for (i=0; i<size; i++) { ... }
for (i=0; i<= size-1; i++) { ... }
UpperCase and LowerCase
• The String class has methods to convert to upper case and lower
case.
• The naming convention is:
• [string name].toUpperCase();
• and
• [string name].toLowerCase();
• For example, if we have a string called lwrCase and we want to
change its contents to upper case, we can use the following code:
• String toUCase = lwrCase.toUpperCase();
• Note that we store the result in a new string called to UCase
Other Useful Methods
• split(<delimiters>) to split a given some delimiter, for example
split a string on spaces or _ (underscores)
• substring(<index>) or substring(<indexStart>, <indexEnd>)
returns part of a string given the indices.
Manipulating Strings
A String is immutable - that is once a String object is created, we
cannot change it.
We can read individual characters but cannot: add, delete or
modify characters.
We use StringBuffer to manipulate strings:
1. Replacing characters
2. Appending a string with another string / character
3. Delete a portion of a string, etc