How to change spaces to underscore and make string case insensitive?

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


You can use the replaceAll & toLowerCase methods but keep in mind that they don't change the string (they just return a modified string) so you need to assign the back to the variable, eg.

String itemname = bundle.getString("itemname"); 
itemname = itemname.replaceAll(" ", "_").toLowerCase(); 
String filename = itemname + ".html";