Home / Questions / Write a statement each to perform the following tasks on a string: (i) Find and display the position of the last space in a string s.(ii) Convert a number stored in a string variable x to double data type.
Explanatory Question

Write a statement each to perform the following tasks on a string:

(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type.

👁 89 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Statements for string operations:

(i) To find and display the position of the last space in a string s:


int lastSpaceIndex = s.lastIndexOf(' ');
System.out.println(lastSpaceIndex);

(ii) To convert a number stored in a string variable x to double data type:


double num = Double.parseDouble(x);