Examples of the replaceFirst method

To replace only the first semi-colon with a hyphen, we can use the following:

input = input.replaceFirst(";", "-"); 

What will be the output if we have to use the replaceFirst method instead of replaceAll in the input text of "$%apple% $%banana% $%orange%" for escaping the dollar signs?

The code will become as follows:

input = input.replaceFirst("\$", "\\\$"); 

It will replace only the first $ sign; hence, the output will only have the first $ escaped as follows:

$%apple% $%banana% $%orange% 

To replace the first dot of an IPV4 IP address with a colon, we can use the following code:

String newip = ipaddress.replaceFirst("\.", ":"); 

Only the first dot will be replaced by a colon; hence, an IP value of 10.11.22.123 will become 10:11.22.123.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.120