Question

In: Computer Science

The Strings Object in particular is full of useful classes and methods. You can parse and...

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?

Solutions

Expert Solution

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


Related Solutions

It the two methods of measuring molecular weight that can produce full molecular weight distributions? For...
It the two methods of measuring molecular weight that can produce full molecular weight distributions? For each of these techniques, describe the major limitations of the technique? Size exclusion chromatography and Mass Spectrometry those are the methods describe the major limitations of the techniques ????
It the two methods of measuring molecular weight that can produce full molecular weight distributions? For...
It the two methods of measuring molecular weight that can produce full molecular weight distributions? For each of these techniques, describe the major limitations of the technique? Size exclusion chromatography and Mass Spectrometry those are the methods describe the major limitations of the techniques ????
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods. -Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract. -Extend Customer to two subclasses: FlightCustomer, and RetailCustomer -FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. You may not add any more fields to the class. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initialize an empty queue.    */    public StringQueue() { //TODO   ...
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G,...
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of ACGTACGTTT, there are...
Some methods of assessment appear to be more useful than others. If you were starting your...
Some methods of assessment appear to be more useful than others. If you were starting your own business and looking into hiring Executive Assistant, which assessment methods would you use and why? What process would you go through to establish which assessment methods to us?
Some methods of initial assessment appear to be more useful than others. If you were starting...
Some methods of initial assessment appear to be more useful than others. If you were starting your own business, which initial assessment methods would you use and why? Be sure to reflect your knowledge of ideas presented in Chapter eight as you develop your own suggestions. Keep in mind the necessity of being able to measure assessments.
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
How can you be sure that accounting income number can be useful (in terms of research)
How can you be sure that accounting income number can be useful (in terms of research)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT