I have following question. In my app there is a listview. I get itemname from listview and transfer it to the webview as a string. How to ignore case of this string and change spaces to underscores?
For example: String itemname = "First Topic"
. I transfer it to the next activity and want to ignore case and change space to underscore (I want to get first_topic
in result). I get "itemname" in webviewactivity and want to do what I've described for following code:
String filename = bundle.getString("itemname") + ".html";
Please, help.
Use the String. replaceAll method to replace all spaces with underscores in a JavaScript string, e.g. string. replaceAll(' ', '_') . The replaceAll method returns a new string with all whitespace characters replaced by underscores.
Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.
use replaceAll
and toLowerCase
methods like this:
myString = myString.replaceAll(" ", "_").toLowerCase()
This works for me:
itemname = itemname.replaceAll("\\s+", "_").toLowerCase();
replaceAll("\\s+", "_")
replaces consecutive whitespaces with a single underscore.
"first topic".replaceAll("\\s+", "_")
-> first_topic
"first topic".replaceAll(" ", "_")
-> first__topic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With