Try .replace(/ /g,"_");
Edit: or .split(' ').join('_') if you have an aversion to REs
Edit: John Resig said:
Answer from Crescent Fresh on Stack OverflowIf you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)
Why Use an Underscore Remover?
How Does the Remove Underscores Tool Work?
Why You May Not Want Underscores in Your Text
Try .replace(/ /g,"_");
Edit: or .split(' ').join('_') if you have an aversion to REs
Edit: John Resig said:
If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)
try this:
key=key.replace(/ /g,"_");
that'll do a global find/replace
javascript replace
Perfect candidate for a token cycle. EDITED to add lowercasing, as requested.
\documentclass{article}
\usepackage{tokcycle}
\Characterdirective{\addcytoks[x]{\explowerchar{#1}}}
\Spacedirective{\addcytoks{\textunderscore\allowbreak}}
\def\explowerchar#1{%
\ifcase\numexpr`#1-`A\relax
a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or k\or l\or m\or
n\or o\or p\or q\or r\or s\or t\or u\or v\or w\or x\or y\or z\else
#1\fi
}
\newcommand{\questionsection}{Game of rolling dice, plus this should
allow line breaks as well. Let us see if it does}
\newcommand{\questionsectionfiltered}{\expandafter\tokencyclexpress
\questionsection\endtokencyclexpress}
\begin{document}
\questionsectionfiltered
\end{document}

You can use expl3 that's more robust than xstring (and more flexible).
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\filter}{m}
{
\tabraham_filter:n { #1 }
}
\tl_new:N \l__tabraham_filter_text_tl
\cs_new_protected:Nn \tabraham_filter:n
{
% set the tl variable to the lowercased version of the input
\tl_set:Nx \l__tabraham_filter_text_tl { \text_lowercase:n { #1 } }
% replace spaces with underscores
\tl_replace_all:Nnn \l__tabraham_filter_text_tl { ~ } { \_ }
% deliver the result
\tl_use:N \l__tabraham_filter_text_tl
}
\ExplSyntaxOff
\newcommand{\questionsection}{Game of rolling dice}
\begin{document}
\filter{\questionsection}
\filter{Game of rolling dice}
\end{document}

You may want to define a filtered version of some token list, so when using it you don't have to repeat the whole process.
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\filter}{m}
{
\tabraham_filter:nn { #1 } { \tl_use:N }
}
\NewDocumentCommand{\newfilteredcommand}{mm}
{
\tl_if_exist:NF #1
{
You~don't~want~to~silently~redefine~something,~do~you?
}
{
\tl_new:N #1
\tabraham_filter:nn { #2 } { \tl_set_eq:NN #1 }
}
}
\tl_new:N \l__tabraham_filter_text_tl
\cs_new_protected:Nn \tabraham_filter:nn
{
\tl_set:Nx \l__tabraham_filter_text_tl { \text_lowercase:n { #1 } }
\tl_replace_all:Nnn \l__tabraham_filter_text_tl { ~ } { \_ }
#2 \l__tabraham_filter_text_tl
}
\ExplSyntaxOff
\newcommand{\questionsection}{Game of rolling dice}
\newfilteredcommand{\questionsectionfiltered}{\questionsection}
\begin{document}
\filter{\questionsection}
\filter{Game of rolling dice}
\questionsectionfiltered
\texttt{\meaning\questionsectionfiltered}
\end{document}
The last line is to show that \questionsectionfiltered is what's expected.
