String Handling
Monday, 14 October 2024 21:09
Methods:
• charAt()
• contains()
• equals() & equalsIgnoreCase()
• compareTo() & compareToIgnoreCase()
• length()
• indexOf() & lastIndexOf()
• substring()
• startsWith() & endsWith()
charAt()
• returns a character at a specific index
• syntax: string.charAt(index)
Index:
Think of a string as a container of characters
The index is the number assigned to that container
Index: a number used to reference the character at the position in the string
The last index in a string will always be: length - 1
e.g. if the length of a string is 8
The last index will be 7
String phrase = "SoupBoys";
char first = phrase.charAt(0); -> result: S
char last = pharse.charAt(7); -> result: s
char fourth = phrase.charAt(3); -> result: p
e.g. string length is variable/we don't know the length of the string
int len = string.length();
Getting the last character:
char last = phrase.charAt(len - 1);
Remember: the last character is always at length - 1
NOTE: One space = One character
indexOf() & lastIndexOf() -> parameter: string
• Returns an integer position of a phrase or a character depending on the given input, if not
found, it returns -1.
indexOf()
String name = "Shriyan";
int indexA = name.indexOf("a");
result: 5
I can also group multiple consecutive characters together and it will be grouped under that
one index.
e.g. using Shriyan
int indexAn = name.indexOf("an");
-> each character is individually indexed until the group
S - 0, h - 1, r - 2, i - 3, y: 4, an-5
What happened is the group takes on the index of the leading character of the group, in this
case: 'a' is the leading character
indexOf(character, startPosition)
• this is useful when there is repeated characters and you want to skip the first instance of
that character
Character only-> always returns the first instance of the character:
String name = "Yashika";
int indOfA = name.indexOf("a");
->result: 1
Character and StartPos
• using 2 as the start position
• int indOfA2 = name.indexOf("a", 2);
• it starts from index 2 and goes to the end of the string.
String name = "Shriyan Naidoo";
int indexOfA2 = name.indexOf("a", 6);
result: 9
Note: Even with the start position, it will still return the instance of the first related character it
found.
lastIndexOf(char)
• as stated in the name, return the last index of the character's occurrence in the string
e.g. String name = "Yashika";
int lastA = name.lastIndexOf("a"); ->result: 6
contains(string)
The contains method is used to check if one string contains a sequence of characters or one
specific character in the string.
This method is used in if statements.
This returns a boolean as if the string contains the sequence, it returns true, else it is false.
String name = "Shriyan";
if (name.contains("a"))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
Outcome: Yes
Key points to note: in the question they will ask whether a string contains a
character/sequence also if it is a part of-> use it then
equals() & equalsIgnoreCase()
equals(): used when we want case sensitivity
This means that Yashika != yashika
Note: in the question they will tell you whether or not the string is case sensitive
When to use: check if the strings match/equal
String n1 = "Shriyan";
String n2 = "shriyan";
NOTE: Case Sensitive
if (n1.equals(n2))
{
System.out.println("Equal");
}
else
{
System.out.println("Unequal");
}
Result: Unequal
String n1 = "Shriyan";
String n2 = "shriyan";
NOTE: Case Sensitive
if (n1.equalsIgnoreCase(n2))
{
System.out.println("Equal");
}
else
{
System.out.println("Unequal");
}
Result: Equal
startsWith()/endsWith()
startsWith-checks to see if a string starts with a specific character or sequence of characters.
endsWith-checks to see if a string ends in a specific character or sequence of characters.
Case Sensitive.
String name = "Shriyan";
if (name.startsWith("S") && name.endsWith("N"))
{
System.out.print(name);
}
else
{
System.out.print("No Name");
}
result: No Name
substring()
This is used when we want to extract a part of a string.
Key words: extract, cut off, take out
Two types of substrings
1. When we end in the middle
Syntax: string.substring(startIndex, endIndex + 1);
String name = "Shriyan Naidoo";
String firstName = name.substring(0, 7);
2. When it ends at the end
Syntax: string.substring(startIndex);
String name = "Shriyan Naidoo";
String surname = name.substring(8);
compareTo(string)/compareToIgnoreCase(string)
Mainly used in the sort method to sort out string in alphabetical order.
e.g. Students array -> name, grade
sort the students by their names alphabetically (> 0)
reverse (< 0)
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
if (sArr[i].getName().compareToIgnoreCase(sArr[j].getName()) > 0)
{
Students temp = sArr[i];
sArr[i] = sArr[j];
sArr[j] = temp;
}
}
}