top of page
Writer's picturePraveen Mogan

Strings

Another easy unit ahead, similar to arrays, but also different. Strings are ways to represent text within Java. Essentially the String objects store multiple characters, connected together. Thus, these objects can be manipulated as well to produce the desired change needed. What are some of these changes you ask? Well, you can remove all the consonants in a sentence, or all of the vowels. Maybe you want to tYpe LiKE ThIs. Yikes, hopefully not! Nonetheless, let's get into the unit. Strings can be a lot of fun!

 

As always, a list of terms that you need to know:

Strings - immutable objects that store and manipulate text. The text in the String is stored as a character Array.

Immutable Objects - objects that can't be changed, like Strings

 

Not much this time, but still extremely important! You need to know that Strings are immutable. So, when you "change" the value of a String, you are really just creating another String object that stores different characters. Tricky, right?

 

With that said here are some basic syntactical rules that you need to know about Strings:


Creating a String:

String name = new String("enter text here");

String bob = new String("Bob");


Shortcut:

String name = "enter text here";

String bob = "Bob";

 

Now let's get into the bulk of the lesson today: String methods. Strings need to be changed or altered in multiple ways, so welcome to the world of String methods. There are a lot, but knowing them will come in really handy in the future.


toCharArray() - returns a char Array that stores the same text as the String

String s = "turtles";

char[] letters = s.toCharArray();

System.out.println(letters[2]); //output = r


length() - returns the length of the String

String s = "apples";

System.out.println(s.length()); // output = 6


charAt(int x) - returns the character at the given index. Error if index out of bounds.

String s = "Bob";

System.out.println(s.charAt(0)); //output = B


toUpperCase() - returns a new String that is an all uppercase version of the calling String

String s = "tUrtles";

System.out.println(s.toUpperCase()); //output = TURTLES


toLowerCase() - returns a new String that is an all-lowercase version of the calling String

String s = "tUrtles";

System.out.println(s.toLowerCase()); //output = turtles


equals(String other) - returns true when the received String and the calling String are the exact same and false otherwise

String t = "apples";

String a = "Apples";

String b = "apples";

System.out.println(t.equals(t)+t.equals(a)+t.equals(b)); //output = true false false


equalsIgnoreCase(String other) - returns true when the received String and the calling String are the exact same and false otherwise (non-case sensitive)

String t = "apples";

String a = "Apples";

String b = "apples";

System.out.println(t.equalsIgnoreCase(t)+t.equalsIgnoreCase(a)+t.equalsIgnoreCase(b)); //output = true true true


compareTo(String other) - returns an int value that returns how far apart the first letter that is different between the calling and received String really is

- positive - received String is the first alphabetically

- negative - calling String is the first alphabetically

- zero - they are the same String


contains(String other) - returns true when the received String exists in the calling String and false otherwise

System.out.println("bob".contains("ob")); //output = true


startsWith(String other) - returns true if the calling String starts with received String

System.out.println("bob".startsWith("bo")); //output = true


endsWith(String other) - returns true if the calling String ends with received String

System.out.println("bob".startsWith("ob")); //output = true


indexOf(String other) - returns the 1st index of the received String, -1 if not found

String s= "apples";

s.indexOf("pl"); //output = 2


indexOf(String other, int firstIndex) - returns the first index of the received String, starting from the given index. -1 if not found.

String s= "apples";

s.indexOf("pl",0); //output = 2

s.indexOf("pl",3); //output = -1


substring(int startingIndex) - returns a String created from the calling String starting at the given index and going all the way to the end

String s = "apples";

s.substring(2); //output = ples


substring(int startingIndex, int endingIndex) - returns a String created from the calling String starting at the starting index and ending at the position before the end

String s ="apples";

s.substring(1,4); //output = ppl


split(String s) - returns a String array of the calling String created by using the received String as a spacer.

String s = "aa*cc*bb";

String[] letters = s.split("*");

letters[0] //aa

letters[1] //cc

letters[2] //bb

 

Wow, a lot of methods, I know! But, at least now you know all the String methods that will be tested on the AP Exam. Good Job! 2 more units, and you are done with AP Computer Science A.


I hope it helps and happy coding/cramming! Until next time.

34 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page