Are there purely expandable variants of \MakeUppercase?

Updated answer

For expl3 based partly on ideas raised here in my original approach and in Bruno's method we have now developed a set of expandable case-changing functions that implement case mappings as described by the Unicode Consortium:

  • \str_foldcase:n
  • \text_uppercase:n(n)
  • \text_lowercase:n(n)
  • \text_titlecase:n(n)

One important point to note is that they work with 'engine native' input, which means just ASCII for pdfTeX (the upper half of the 8-bit range is tricky). For XeTeX/LuaTeX the full Unicode range is covered.

The direct answer to the question is to use \text_uppercase:n: it does expansion of input in a selective way, can deal with entries such as \aa and with work inside an expansion context including 'f-type' methods (expansion using \romannumeral). In the current implementation there are features very similar to the textcase package, for example selective skipping of input, skipping over math mode material, etc.

There are four types of function to cover different use cases:

  • 'Removal' of case for use in non-text contexts. This looks rather like 'lower casing' and is a one-one mapping. As the data is string-like the function is called \str_foldcase:n and does not skip or expand any input.

  • Uppercasing

  • Lowercasing

  • Making 'titlecase' (Unicode description): it covers only the first 'letter' of some text not the first letter of every word of some text (the latter is usually called title case in English)

The code includes the ability to handle context dependence (e.g. final-sigma in Greek) and also language-dependent versions such as \text_lowercase:nn { tr } { I } to apply Turkish rules (here producing a dotless-i).

At the implementation level, the approach taken is to map over the input using a two-part strategy, first working out if the next token is a space, something braced or something else (what we call N-type). Each type can be grabbed properly and then case changed as appropriate using a lookup table.

Note that using Lua in LuaTeX offers only a partial solution for two reasons. First, Lua does not work with TeX tokens meaning that for example skipping math mode input requires more effort. Secondly, the current Lua Unicode library available in LuaTeX is poorly documentation and does not cover context-dependent issues, non one-one mappings and so on. For example, a simple test case is

\documentclass{article}
\usepackage{fontspec}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\text_uppercase:n { Fußball }
\ExplSyntaxOff

\directlua{tex.print(unicode.utf8.upper("Fußball"))}
\end{document}

where no case changing occurs in the Lua-based case. (It is also not clear what Unicode version the Lua library follows.)


Original answer

For expl3, I wrote the following as the most robust approach I could find

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new:Npn \tl_to_upper_case:n #1
  { \exp_args:Nf \__tl_to_upper_case:n {#1} }
\cs_new:Npn \__tl_to_upper_case:n #1
  { \__tl_to_upper_case:w #1 ~ \q_no_value \q_stop }
\cs_new:Npn \__tl_to_upper_case:w #1 ~ #2 \q_stop
  {
    \quark_if_no_value:nTF {#2}
      { 
        \tl_map_function:nN {#1} \__tl_to_upper_case_aux:N 
        \tl_trim_spaces:n { }
      }
      { \__tl_to_upper_case:w #1 { ~ } #2 \q_stop }
  }
\cs_new:Npn \__tl_to_upper_case_aux:N #1
  {
    \prg_case_str:nnn {#1}
      {
        { a } { \__tl_to_case_aux:nw { A } }
        { b } { \__tl_to_case_aux:nw { B } }
        { c } { \__tl_to_case_aux:nw { C } }
        { d } { \__tl_to_case_aux:nw { D } }
        { e } { \__tl_to_case_aux:nw { E } }
        { f } { \__tl_to_case_aux:nw { F } }
        { g } { \__tl_to_case_aux:nw { G } }
        { h } { \__tl_to_case_aux:nw { H } }
        { i } { \__tl_to_case_aux:nw { I } }
        { j } { \__tl_to_case_aux:nw { J } }
        { k } { \__tl_to_case_aux:nw { K } }
        { l } { \__tl_to_case_aux:nw { L } }
        { m } { \__tl_to_case_aux:nw { M } }
        { n } { \__tl_to_case_aux:nw { N } }
        { o } { \__tl_to_case_aux:nw { O } }
        { p } { \__tl_to_case_aux:nw { P } }
        { q } { \__tl_to_case_aux:nw { Q } }
        { r } { \__tl_to_case_aux:nw { R } }
        { s } { \__tl_to_case_aux:nw { S } }
        { t } { \__tl_to_case_aux:nw { T } }
        { u } { \__tl_to_case_aux:nw { U } }
        { v } { \__tl_to_case_aux:nw { V } }
        { w } { \__tl_to_case_aux:nw { W } }
        { x } { \__tl_to_case_aux:nw { X } }
        { y } { \__tl_to_case_aux:nw { Y } }
        { z } { \__tl_to_case_aux:nw { Z } }
      }
      { \__tl_to_case_aux:nw {#1 } }
  }
\cs_new:Npn \__tl_to_case_aux:nw #1#2 \tl_trim_spaces:n #3
  {
   #2
   \tl_trim_spaces:n { #3 #1 }
  }
\cs_set_eq:NN \MakeExpandableUppercase \tl_to_upper_case:n
\ExplSyntaxOff
\begin{document}
\MakeExpandableUppercase{Hello World}
\edef\test{\MakeExpandableUppercase{Hello World}}
\show\test
\MakeExpandableUppercase{Hello {World}}
\edef\test{\MakeExpandableUppercase{Hello {World}}}
\show\test
\edef\test{Hello\space\space World}
\MakeExpandableUppercase{\test}
\edef\test{\MakeExpandableUppercase{\test}}
\end{document}

The reason for the space stripping at the end of the input is that you can't avoid it at the start of the string, so I felt the best you could do was say 'spaces at the ends are stripped'. Spaces should be retained within the input. You can implement a lower case function in the same way, and if you do nesting

 \MakeExpandableUppercase{\MakeExpandableLowercase{Hello} World}

should work correctly. As illustrated by the last example, material is expanded before doing the case change. That applies even to protected macros, as the underlying expansion uses \romannumeral. So the argument needs to be made up of purely expandable material.

(As a note, this can of course be implemented without expl3.)


For completeness, a LuaTeX solution might read

\documentclass{article}
\usepackage{fontspec}
\newcommand*\MakeExpandableUppercase[1]{%
  \scantokens\expandafter{%
    \directlua{
      tex.write(string.upper("\luatexluaescapestring{\unexpanded{#1}}"))
    }%
  \noexpand
  }%
}%
\begin{document}
\MakeExpandableUppercase{hello world \oe}
\end{document}

(I'm no Lua expert: there may be a more efficient approach.)


Edit3: now the token list module in LaTeX3 provides \text_uppercase:n and \text_lowercase:n, which stem from that discussion but are more robust and much less greedy on the number of control sequences. Slower, as well.

EDIT2: after a first code which ate spaces and choked when it saw braces, and a second code which would crash for more than 600 or so tokens, I spent some time writing a clean code that still works with >5k tokens, although it gets slow. The new code actually lends itself to all sorts of generalizations (see near the bottom of the code). I got rid of the expansion control that was the cause of an "too many levels of expansion", and the code is now much less tricky.

(Sorry, code and explanations are long.) Now, after exactly three expansion steps, \Uppercase{ Hel{l }o\error World } expands to HEL{L }O\error WORLD, with spaces, braces, and macros kept (and not expanded).

Two ideas:

  • Check for braces and for spaces by using a delimited argument (see \UL_brace_check:nw and \UL_space_check:nw), after having placed a {\q_mark} \q_stop after all the tokens, to ensure that there is at least one brace or space after the argument.

  • Define tables of case change. For example, \UL_table_upper_p is a macro which expands to P, and \UL_table_lower_A expands to a. If the relevant entry of the table is not defined, then the token which is being read is not altered. See \UL_convert_token:nN for this. The last part of the code is all about setting up these tables ("case-tables"?).

We need to step inside brace groups and expand \UL_to_case:nn entirely before continuing. For this, we use \romannumeral-\\0`, closed by a space, which is introduced at the very end.

A few macros deserve some explanation.

  • \UL_expand_csname:n{...} replaces every \csname abc\endcsname construction by the corresponding \abc. I need this somewhere to explicit a csname which is quite deep in a definition.

  • \expandafter:nw{...}\foo will expand \foo before ....

  • \expandsome{\foo\expandthis\bar\baz\expandthis\foo{ABC}} will expand the macro following \expandthis once (that macro is allowed to take any kind of argument: in fact, we simply \expandafter it).

The code can also be found online. Finally, the code, with some tests at the end, and a few comments.

\catcode`\_=11\relax
\catcode`\:=11\relax

% ======================== Generic macros

% A few standard commands to manipulate arguments
\long\gdef\use_none:n#1{}
\long\gdef\use_none:nn#1#2{}
\long\gdef\use_i:nn#1#2{#1}
\long\gdef\use_ii:nn#1#2{#2}
\long\gdef\use_ii_i:nn#1#2{#2#1}
\long\gdef\use_ii_iii_i:nnn#1#2#3{#2#3#1}
\long\gdef\use_i_bbii:nn#1#2{#1{{#2}}}
\long\gdef\use_bii_bi:nn#1#2{{#2}{#1}}

% What expl3 calls "quarks", useful for |\ifx| comparisons.
\gdef\q_stop{\q_stop}
\gdef\q_mark{\q_mark}
\gdef\q_nil{\q_nil}
\long\gdef\use_none_until_q_stop:w#1\q_stop{}

% Two tests 
\long\gdef\UL_if_empty:nTF#1{%
  \expandafter\ifx\expandafter\q_nil\detokenize{#1}\q_nil%
  \expandafter\use_i:nn%
  \else%
  \expandafter\use_ii:nn%
  \fi}

\expandafter\long\expandafter\gdef\expandafter\UL_if_detok_qmark:wTF%
\expandafter#\expandafter1\detokenize{\q_mark}#2\q_stop{% 
  \UL_if_empty:nTF{#1}}

% ======================== Main command: |\UL_to_case:nn|
% Usage:       |\UL_to_case:nn{<table>}{<text>}|
% Expands in:  2 steps.
\long\gdef\UL_to_case:nn{\romannumeral\UL_to_case_aux:nn}
\long\gdef\UL_to_case_aux:nn#1#2{-`\0%
  \UL_brace_check:nw{#1}#2{\q_mark} \q_stop\UL_to_case_end:n{}}%

% Initially, I used |\q_mark{} \q_stop|: the braces and space are there
% to avoid runaway arguments in |\UL_brace_check:nw| and 
% |\UL_space_check:nw|, whose "w" arguments are delimited respectively
% by an open brace, and by a space. I changed to |{\q_mark} \q_stop|:
% then we only do the check for |\q_mark| in the case of a brace group, 
% and not at every step.

% |\UL_to_case_output:n| appends its argument to the argument of
% |\UL_to_case_end:n|.
\long\gdef\UL_to_case_output:n#1#2\UL_to_case_end:n#3{%
                                    #2\UL_to_case_end:n{#3#1}}
\long\gdef\UL_to_case_end:n#1{ #1}
% And |\UL_to_case_end:n| expands to 
% - a space, which stops the expansion of |\romannumeral-`\0|,
% - followed by its argument, which is the result we want.


% First, we check whether the next token is a brace. 
\long\gdef\UL_brace_check:nw#1#2#{%
  \UL_if_empty:nTF{#2}%
  {\UL_brace_yes:nn{#1}}%
  {\UL_space_check:nw{#1}#2}%
}
% If there is a brace, we might have reached {\q_mark}.
\long\gdef\UL_brace_yes:nn#1#2{%
  \expandafter\UL_if_detok_qmark:wTF \detokenize{#2 \q_mark}\q_stop{% 
    \use_none_until_q_stop:w% 
  }{% 
    \csname UL_table_#1_braces\endcsname{#1}{#2}%
    \UL_brace_check:nw{#1}%
  }%
}

% Then check whether the next token is a space.
\long\gdef\UL_space_check:nw#1#2 {%
  \UL_if_empty:nTF{#2}%
  {\UL_convert_token:nn{#1}{ }}%
  {\UL_convert_token:nn{#1}#2 }% we put the space back!
}

\long\gdef\UL_convert_token:nn#1#2{%
  \ifcsname UL_table_#1_\detokenize{#2}\endcsname%
  \expandafter\use_i:nn%
  \else%
  \expandafter\use_ii:nn%
  \fi% 
  {\csname UL_table_#1_\detokenize{#2}\endcsname}%
  {\csname UL_table_#1_default\endcsname{#2}}%
  \UL_brace_check:nw{#1}% Do the next token.
}


% ======================== Casecode tables.
% ============ Generic setup.
% Typical use:
% - |\UL_setup:nnn{u}{a}{A}| to define |a| uppercased as |A|.
% - |\UL_setup_cmd:nnpn{ULnil}{\NoCaseChange}#1{%
%      \UL_to_case_output:n{#1}}|
% Note that for the second, we have to grab all the arguments in one go.
% Also note that the second should not be used until we define the ULec 
% and ULea tables below.
%
% - |\UL_set_eq:nnnn{tableA}{tokenA}{tableB}{tokenB}| sets the entry
% |tokenA| of the table |tableA| to be equal to the entry |tokenB| of the
% table |tokenB|.
% - |\UL_new_table:nn{tableA}{tableB}| creates a new table, |tableA|, 
% which is a copy of |tableB|.

\protected\long\gdef\UL_content_of_table_add:nn#1#2{%
  \long\expandafter\gdef\csname UL_table_#1%
  \expandafter\expandafter\expandafter\endcsname%
  \expandafter\expandafter\expandafter{%
    \csname UL_table_#1\endcsname{#2}}%
}

\protected\long\gdef\UL_setup:nnn#1#2#3{%
  \UL_content_of_table_add:nn{#1}{#2}%
  \expandafter\long\expandafter\gdef%
  \csname UL_table_#1_\detokenize{#2}\endcsname%
  {\UL_to_case_output:n{#3}}%
}

\protected\long\gdef\UL_setup_cmd:nnpn#1#2#3#{%
  \UL_content_of_table_add:nn{#1}{#2}%
  \UL_expand_csname:n{%
    \long\gdef\csname UL_table_#1_\detokenize{#2}\endcsname##1##2{%
      \expandafter:nw{\use_ii_i:nn{##1{##2}}}%
      \csname UL_table_#1_\detokenize{#2}_aux\endcsname}%
  }%
  \use_i_bbii:nn{\expandafter\long\expandafter\gdef%
    \csname UL_table_#1_\detokenize{#2}_aux\endcsname#3}%
}

\protected\long\gdef\UL_set_eq:nnnn#1#2#3#4{%
  \UL_content_of_table_add:nn{#1}{#2}%
  {\expandafter}\expandafter\global\expandafter\let%
  \csname UL_table_#1_\detokenize{#2}\expandafter\endcsname%
  \csname UL_table_#3_\detokenize{#4}\endcsname%
}

\long\gdef\UL_new_table:nn#1#2{%
  \ifcsname UL_table_#1\endcsname%
  \PackageError{ULcase}{Table \detokenize{#1} already defined!}{}%
  \fi%
  \long\expandafter\gdef\csname UL_table_#1\endcsname{}%
  %
  \def\UL_tmpA{#1}%
  \def\UL_tmpB{#2}%
  \expandafter\expandafter\expandafter\UL_new_table_aux:nnn%
  \csname UL_table_#2\endcsname{}%
}
\long\gdef\UL_new_table_aux:nnn#1{%
  \UL_if_empty:nTF{#1}{}{%
    \UL_set_eq:nnnn{\UL_tmpA}{#1}{\UL_tmpB}{#1}%
    \UL_new_table_aux:nnn%
  }%
}%
\long\gdef\UL_new_table:n#1{\UL_new_table:nn{#1}{ULnil}}




% ============ Table ULea, \expandafter:nw
% 
% The |ULea| table puts |\expandafter| after each token (including braces
% and spaces). Allows us to define |\expandafter:nw|, which expands what
% follows its first argument once. 
% 
% |\expandafter:nw| takes 2-steps to act. For a 1-step version, use 
% |\MEA_trigger:f\MEA_expandafter:nw|. 

\long\gdef\UL_table_ULea_default#1{\UL_to_case_output:n{\expandafter#1}}%
\long\gdef\UL_table_ULea_braces#1#2{%
  \expandafter\expandafter\expandafter\UL_to_case_output:n%
  \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter\expandafter%
    \expandafter\expandafter\expandafter{%
      \UL_to_case:nn{#1}{#2}\expandafter}%
  }%
}
\let\MEA_trigger:f\romannumeral
\def\MEA_expandafter:nw{\UL_to_case_aux:nn{ULea}}
\def\expandafter:nw{\MEA_trigger:f\MEA_expandafter:nw}


% ============ Table |ULec|, |\UL_expand_csname:n|
% The |ULec| table expands only the 
% |\csname ...\endcsname| constructions.
% 
\long\gdef\UL_table_ULec_default{\UL_to_case_output:n}%
\long\gdef\UL_table_ULec_braces#1#2{%
  \expandafter\expandafter\expandafter\UL_to_case_output:n%
  \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter{\UL_to_case:nn{#1}{#2}}%
  }%
}
\long\expandafter\gdef\csname%
  UL_table_ULec_\detokenize{\csname}\endcsname#1#2{%
  \expandafter:nw{\use_ii_iii_i:nnn{#1{#2}}}%
  \expandafter\UL_to_case_output:n\csname%
}%

\def\UL_expand_csname:n{\MEA_trigger:f\UL_to_case_aux:nn{ULec}}


% ============ Table |ULexpandsome|, |\expandsome|
% The |ULexpandsome| table expands only the tokens following |\expandthis|.
% 
\long\gdef\UL_table_ULexpandsome_default{\UL_to_case_output:n}%
\long\gdef\UL_table_ULexpandsome_braces#1#2{%
  \expandafter\expandafter\expandafter\UL_to_case_output:n%
  \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter{\UL_to_case:nn{#1}{#2}}%
  }%
}
\long\expandafter\gdef\csname%
  UL_table_ULexpandsome_\detokenize{\expandthis}\endcsname#1#2{%
  \expandafter:nw{#1{#2}}%
  %\expandafter\UL_to_case_output:n\csname%
}%

\def\expandsome{\MEA_trigger:f\UL_to_case_aux:nn{ULexpandsome}}


% ============ The default table, ULnil
\long\gdef\UL_table_ULnil{{default}{braces}{$}}%$
\long\gdef\UL_table_ULnil_default{\UL_to_case_output:n}
\long\gdef\UL_table_ULnil_braces#1#2{%
  \expandafter\expandafter\expandafter\UL_to_case_output:n%
  \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter{\UL_to_case:nn{#1}{#2}}%
  }%
}
\UL_setup_cmd:nnpn{ULnil}{\NoCaseChange}#1{%
  \UL_to_case_output:n{#1}}


% ============ Working on math mode.
% 
% We add \q_mark so that \UL_dollar_aux:nw can read to the next dollar
% without unbracing the argument, so that ${...}$ --x-> $...$
\long\expandafter\gdef\csname UL_table_ULnil_\detokenize{$}\endcsname#1#2{%$
    \UL_dollar_aux:nw{#1{#2}}\q_mark%
}
% Grab until the next dollar, so #2={\q_mark Math Stuff}. 
% If \use_none:n #2 is empty, then we had only grabbed `\q_mark`, 
% which means there was $$, and we need to redo the same business. 
% Otherwise, we output, after stripping the \q_mark.
\long\gdef\UL_dollar_aux:nw#1#2${%$%
  \expandafter\UL_if_empty:nTF\expandafter{\use_none:n#2}{% eats \q_mark
    \UL_bidollar:nw{#1}\q_mark%
  }{%
    \expandafter\UL_to_case_output:n\expandafter{%
      \expandafter$\use_none:n#2$}#1%
  }%
}
\long\gdef\UL_bidollar:nw#1#2$${%
  \expandafter\UL_to_case_output:n\expandafter{%
    \expandafter$\expandafter$\use_none:n#2$$}#1}



% =========== Lowercase, Uppercase, Caesar
\long\gdef\Lowercase{\UL_to_case:nn{lower}}
\long\gdef\Uppercase{\UL_to_case:nn{upper}}
\long\gdef\CaesarCipher{\UL_to_case:nn{caesar}}

% Setup the uppercase and lowercase tables.
\UL_new_table:n{lower}
\UL_new_table:n{upper}

\protected\long\gdef\UL_setup_lower_upper:n#1{%
  \UL_if_empty:nTF{#1}{}{%
    \UL_setup:nnn{upper}#1%
    \expandafter:nw{\UL_setup:nnn{lower}}\use_bii_bi:nn#1%
    \UL_setup_lower_upper:n%
  }%
}
% should become user-firendly.
\UL_setup_lower_upper:n {{a}{A}} {{b}{B}} {{c}{C}} {{d}{D}} {{e}{E}} 
{{f}{F}} {{g}{G}} {{h}{H}} {{i}{I}} {{j}{J}} {{k}{K}} {{l}{L}} {{m}{M}} 
{{n}{N}} {{o}{O}} {{p}{P}} {{q}{Q}} {{r}{R}} {{s}{S}} {{t}{T}} {{u}{U}} 
{{v}{V}} {{w}{W}} {{x}{X}} {{y}{Y}} {{z}{Z}} {{\ae}{\AE}} {{\oe}{\OE}} 
{}


% Just for fun, we define the Caesar cipher.
\UL_new_table:n{caesar}
\begingroup
  \lccode`\x=1\relax
  \loop
    \lccode`\X=\numexpr\lccode`\x+2\relax
    \lowercase{\UL_setup:nnn{caesar}{x}{X}}%
    \lccode`\x=\numexpr\lccode`\x+1\relax
  \unless\ifnum\lccode`\x>126\relax
  \repeat
\endgroup
\UL_setup:nnn{caesar}{ }{ }




% ====== Various tests
\long\gdef\checkoutput{\ifx\a\b\message{Correct}\else\show\WRONG\fi}

\long\gdef\expandonce#1{% redefines #1 as #1 expanded once.
  \long\xdef#1{\unexpanded\expandafter\expandafter\expandafter{#1}}}
\def\0{\1}\def\1{\2}\def\2{\3}\def\3{\4}\def\4{\5}


% \Uppercase, \Lowercase, \NoCaseChange work (+ nesting)
% Spaces and braces are fine.
\long\gdef\a{\Uppercase{ Hello, { } W\Lowercase{O}r\NoCaseChange{lD}! }}
\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{ HELLO, { } W\Lowercase{O}RlD! }
\checkoutput

% Another test.
\long\gdef\a{\Lowercase{He l%
    \NoCaseChange{\Uppercase{ Lp\NoCaseChange{ o}}}o }}
\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{he l\Uppercase{ Lp\NoCaseChange{ o}}o }
\checkoutput
\long\edef\a{\a}
\long\gdef\b{he l LP oo }
\checkoutput

% Math works (both $$ and $). Nesting does not break, 
% although we would wish for better (i.e. "Letter"-> "letter").
\long\gdef\a{\Lowercase{{t}ExT, $$\frac{A}{B}$$ and $(\mbox{Letter $A$})$}}
\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{{t}ext, $$\frac{A}{B}$$ and $(\mbox{Letter $A$})$}
\checkoutput

\edef\a{\CaesarCipher{a{b}cdef@ ABCX}}
\edef\b{c{d}efghB CDEZ}
\checkoutput


\long\gdef\a{\Uppercase{%
    \0{ a${} {{abd}+cd}$\0{b$${\d $0$}$$ }}%
    \NoCaseChange{ Ac dD\relax\0ii}i cd }%
}
\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{\0{ A${} {{abd}+cd}$\0{B$${\d $0$}$$ }} %
  Ac dD\relax\0iiI CD }%
\checkoutput



% More on braces, spaces, and expansion (nothing is expanded, 
% as we expect).
\long\gdef\a{\Lowercase{ {} \0 { b{C} {dB\AE~}} \0{\0} }}
\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{ {} \0 { b{c} {db\ae ~}} \0{\0} }
\checkoutput

% Testing the ULec table (expanding only \csname)
\long\gdef\a{\UL_expand_csname:n{ \hello 
    {\csname Hdsf\endcsname}##1\space \csname el\endcsname{ }lo, my name}}
\expandonce\a\expandonce\a
\long\gdef\b{ \hello {\Hdsf}##1\space \el{ }lo, my name}
\checkoutput


% Custom table.
\UL_new_table:n{mytable}
\UL_setup:nnn{mytable}{h}{Hello}
\long\gdef\a{\UL_to_case:nn{mytable}{h{ h} {}\space \h}}
\expandonce\a\expandonce\a\expandonce\a\expandonce\a
\long\gdef\b{Hello{ Hello} {}\space \h}
\checkoutput


\def\mydo#1#2{(#1)-(#2)}
\long\gdef\a{\expandsome{\0\0{\expandthis\mydo{\0\expandthis\0}\0\0}}}
\expandonce\a\expandonce\a
\long\gdef\b{\0\0{(\0\1)-(\0)\0}}
\checkoutput

\long\gdef\a{\Uppercase{\NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. \NoCaseChange{The quick brown fox jumps over the lazy dog.} The quick brown fox jumps over the lazy dog. }}
\begingroup\tracingall\tracingonline=0\relax
\expandonce\a\expandonce\a\expandonce\a
\endgroup
\long\gdef\b{The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. The quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. }
\checkoutput

I use the following to get a fully expanded string with the first letter capitalized. I needed it to write the string to the AUX file as part of a message. It was posted long ago by Dan Luecking on CTT. The command \makefirstcap store the expanded string in \firstcaphold. You can make your own varients of this.

\documentclass{article}

\def\makefirstcap#1#2\nil{%
    \iffalse{\fi
    \uppercase{\edef\firstcaphold{\iffalse}\fi#1}#2}}

\begin{document}
\makefirstcap test\nil
\show\firstcaphold
\end{document}