1 Use the API for the string class javalangstring to answer

1. Use the API for the string class (java.lang.string) to answer the following questions. Assume that string1 and string2 are variables declared as type String. Also assume that num is an int variable. Write a complete Java statement as an answer for each question:
a. How would you find the first position in a string of another string?

b. How would you find the last position in a string of another string?

c. How would you remove leading and trailing spaces from a string?

d. How do you get the number of characters in a string?

e. How can you tell if two strings have the same characters?

Solution

a. How would you find the first position in a string of another string?

Answer: string1.charAt(0);

b. How would you find the last position in a string of another string?

Answer: string1.charAt(string1.length()-1);

c. How would you remove leading and trailing spaces from a string?

Answer: string1.trim();

d. How do you get the number of characters in a string?

Answer: int num = string1.length();

e. How can you tell if two strings have the same characters?

Answer: string1.equals(string2);

1. Use the API for the string class (java.lang.string) to answer the following questions. Assume that string1 and string2 are variables declared as type String.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site