Extract First Character (allowing for extra grouping)
You should use the \StrRemoveBraces
before \StrChar
:
\newcommand*{\ExtractFirstChar}[1]{%
\StrRemoveBraces{#1}[\FirstChar]%
\StrChar{\FirstChar}{1}[\FirstChar]%
First char of '#1' is '\FirstChar'\par
}
Assuming your input only contains (non TeX special) ASCII characters and braces:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\firstchar}{sm}
{
\IfBooleanTF{#1}
{
\grill_firstchar:V #2
}
{
\grill_firstchar:n { #2 }
}
}
\cs_new:Nn \grill_firstchar:n
{
\int_compare:nTF { \tl_count_tokens:f { \tl_head:n { #1 } } > 1 }
{% we have to redo
\grill_firstchar:f { \tl_head:n { #1 } }
}
{% just one token
\tl_head:n { #1 }
}
}
\cs_generate_variant:Nn \tl_count_tokens:n { f }
\cs_generate_variant:Nn \grill_firstchar:n { V, f }
\ExplSyntaxOff
\begin{document}
\firstchar{abc} should print ``a''
\firstchar{{a,b,c}} should print ``a''
\firstchar{{{a},b,c}} should print ``a''
\newcommand\test{{{a}},b,c}
\edef\result{\firstchar*{\test}} \result\ should print ``a''
\end{document}