String to lowercase and replace spaces with underscore

substitutiontextcasexstring

I am trying to convert a predefined string in a variable to its lowercase and replace the spaces to underscore. But it's not working. The new string after conversion is
like this:

gameΩ_ofΩ_Ω_rollingΩ_Ω_Ω_dice

Desired output is:

game_of_rolling_dice

Can any one help me out please?

\documentclass{article}
\usepackage{xstring}
\usepackage{textcase}

\newcommand{\questionsection}{Game of  rolling   dice}
\newcommand{\questionsectionfiltered}{\StrSubstitute{\questionsection}{ }{\textunderscore}[\SUBtemp]\MakeLowercase{\SUBtemp}}

\begin{document}
    \questionsectionfiltered
\end{document}

Best Answer

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}

enter image description here

Related Question