127k views
0 votes
What will be displayed on the screen after the following code is executed?

String str = "Plan to sing today.";
String newStr = str.replace("to", "2");
System.out.println(newStr.length());

2 Answers

1 vote

Answer:

The number 17

Step-by-step explanation:

The character str string contains 19 characters.

When the command str. replace("to", "2"); it basically is replaces all sub string to with 2 so that we have "to"occuring on 2 places in a given string. Therefore, it replaces 4 characters with only two characters.

So, after getting the length of the new string, it returns to the number 17.

User Dhobbs
by
5.5k points
1 vote

Answer:

17

Step-by-step explanation:

str string contains 19 character in total.

When we called str.replace("to","2");, it basically replaced all sub string to with 2 so as we have to occurred on 2 places in given string so it replaces 4 characters with 2 characters.

So after getting the length of new string, it returns 17.

User Zpontikas
by
5.3k points