1 of 24

STRINGS

AP CSA

2 of 24

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.

3 of 24

STRING TUTORIALS

4 of 24

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,

5 of 24

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

6 of 24

.length()

String greeting = “Hello”;

Int n = greeting.length();

n = 5

String greeting = “Hello”;

Char last = greeting.charAt(4)

last = ‘o’

7 of 24

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

8 of 24

DISCUSSION

9 of 24

SOLUTIONS

10 of 24

DISCUSSION

11 of 24

SOLUTION

12 of 24

DISCUSSION

13 of 24

SOLUTION

14 of 24

LAB WORK

15 of 24

Formative Assignment - Strings

  1. Write a program that computes your initials from your full name and displays them.
  2. Write a Java program to trim any leading or trailing whitespace from a given string.
  3. Write a Java program to find the second most frequent character in a given string.
  4. An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation.
  5. Write a Java program to replace each substring of a given string that matches the given regular expression with the given replacement.

Sample string : "The quick brown fox jumps over the lazy dog."

In the above string replace all the fox with cat.

Sample Output:

  1. Write a Java program to find longest Palindromic Substring within a string.

Sample Output:

16 of 24

SOLUTIONS

17 of 24

112 PRACTICE QUESTIONS

18 of 24

LAB WORK

19 of 24

SOLUTION

20 of 24

LAB WORK

Explain the reasons for benchmarking in the world of computing.

Find and describe ONE real world example of benchmarking in software design.

21 of 24

LAB WORK

22 of 24

SOLUTION

23 of 24

LAB WORK

24 of 24

SOLUTION