In: Computer Science
The Strings Object in particular is full of useful classes and methods. You can parse and manipulate strings using several methods of the String class. These methods enable you to count characters, find and extract characters and substrings, replace characters and substrings, combine characters and substrings, and compare strings.
Questions to answer:
1. Identify three such methods and discuss how they are used. Then use them in simple relevant examples
2. As you research the String class, what are some of the most
popular uses of substring searching in use today?
3. Are there any uses of strings not covered by the String class or
mentioned in the prompt?
4. Are there other classes that inherit from the String
class?
5. How does the String class extend or support the Array class?
Dear student,
Here are the answers:
1. String class contains a huge number of methods that can be useful in different scenarios. Let us see a few of them:
i. charAt(int index) method: This method is a very important method in the String class as it is used very commonly. This method is used to return the character that is stored at a specific index of a given string with 0 base indexing.
Ex: String str="School";
char c=str.charAt(3);
System.out.println(c);
Output: o
ii.boolean equals() method: This method is used to compare two strings on the basis of each of their characters and return TRUE or FALSE stating that either they are equal or unequal.
Ex: String str1="School";
String str2="Schools";
System.out.println(str1.equals(str2));
Output: False
iii. char[] toCharArray() method: This method is very useful when one wants to edit the string character by character as a string class object is immutable. So, one can store the string in an array format using this method, perform the operations and then again convert it back to a string using the static String valueOf(char[] data) function.
Ex:
String str = "Good Morning";
char a[] = str.toCharArray();// this will create an array 'a' that will store the //string in array format.
2. Substring means a part of a string and this is used to perform search operations and many more. In today's world, substring searching is widely used in classification problems like spam mail or not, relevant news or not, and so on. This is done by breaking a given article(a long string) into each word using substring and then checking each word for keywords that are relevant for classification.
3. No, All uses of a string are covered in the string class except mutability. Mutable strings are provided in a separate class called StringBuffer.
4. String class is a FINAL class that extends Object class. So, it is not inherited by any other class.
5. String class extends Serializable, Comparable<String>, CharSequence.
This means that it is originally an array of characters but work as a single unit and not separate elements. One can see the similarity between an array and a string by its indexing itself.
I hope the given solution helps clear your doubt.
Don't forget to give it a thumbs up.
Regards