STRINGS
AP CSA
Java.lang.String
Char charAT(int index) | Returns the character at the specified location |
Int compareTo(String other) | Returns a negative value if the string comes before other in dictionary order |
Boolean endsWith(String suffix) | Returns true if the string ends with suffix |
Boolean equals(Object other) | Returns true if the string equals other |
Boolean equalsIgnoreCase(String other) | Returns true if the string equals other, except for upper/lowercase distinction |
Int indexOf(String str) | |
Int indexOf(String str, int fromIndex) | Return the start of the first substring equal to str, starting at index 0 or at fromIndex |
Int lastIndexOf(String str) | |
Int lastIndexOf(String str, int fromIndex) | Return the strat of the last substring equal to str, starting at index 0 or at fromIndex |
Int length() | Returns the length of the string |
String replace(char oldChar, char newChar) | Returns a new string that is obtained by replacing all characters oldChar in the string with newChar. |
Boolean startWith(String prefix) | Returns true if the string begins with prefix |
String substring(int beginIndex) | |
String substring(int beginIndex, int endIndex) | Return a new string consisting of alll characters from beginIndex until the end of the string or until endIndex (exclusive) |
String toLowerCase() | Returns a new string with characters converted to lowercase |
String toUpperCase() | Returns a new string with characters converted to uppercase |
String trim() | Returns a new string by eliminating all leading and trailing spaces in the original string. |
STRING TUTORIALS
USING STRINGS OF CHARACTERS
Strings are a sequence of characters, such as “hello”. Java does not have a built in string type. Instead, the Java library contains a predefined class called, naturally enough, String Each quoted string is an instance of a String class
String e = “”; // an empty string
String greeting = “hello”;
String string1 = “hello”;
String string2 = “how are you”;
String message = string1 + string 2;
Concatenation,
EXTRACTING SUBSTRINGS
String greeting = “Hello”;
String mySubString = greeting.substring(0, 4);
Hell
The first parameter of the substring is the first character you want to copy.
The second parameter is the first position you do not want to copy
phrase = greeting.substring(0, 4) + “!”;
Hell!
H | e | l | l | o |
0 | 1 | 2 | 3 | 4 |
.length()
String greeting = “Hello”;
Int n = greeting.length();
n = 5
String greeting = “Hello”;
Char last = greeting.charAt(4)
last = ‘o’
public class Ascii
{
public static void main(String []args)
{
String word = "abcdefg";
int ascii = (int) word.charAt(0);
System.out.println(ascii);
char upperCase = (char) ascii;
System.out.println(upperCase);
}
}
ASCII
DISCUSSION
SOLUTIONS
DISCUSSION
SOLUTION
DISCUSSION
SOLUTION
Formative Assignment - Strings
Sample string : "The quick brown fox jumps over the lazy dog."
In the above string replace all the fox with cat.
Sample Output:
Sample Output:
SOLUTIONS
112 PRACTICE QUESTIONS
LAB WORK
SOLUTION
LAB WORK
Explain the reasons for benchmarking in the world of computing.
Find and describe ONE real world example of benchmarking in software design.
LAB WORK
SOLUTION
LAB WORK
SOLUTION