[Tex/LaTex] Extract First Character (allowing for extra grouping)

charactersstringsxstring

I would like to extract the first character. Using xstring's \StrChar works fine for simple cases. However, when the paramter has an additional brace group, I need some expansion magic to get that to work.

My naive attemps to 1) Test the first char with \IfBeginWith{#1}{\{}{}{} and 2) use \fullexpandarg didn't resolve this issue.

The MWE below produces

enter image description here

where as rhe desired output is a for both cases.

Related Questions:

Code:

\documentclass{article}
\usepackage{xstring}

\newcommand*{\ExtractFirstChar}[1]{%
    %% #1 = string to extract first char from
    %\fullexpandarg
    %\IfBeginWith{#1}{\{}{%
    %    \StrChar{#1}{2}[\FirstChar]%
    %}{%
        \StrChar{#1}{1}[\FirstChar]%
    %}%
    First char of '#1' is '\FirstChar'\par
}

\begin{document}
  \ExtractFirstChar{abc}
  \ExtractFirstChar{{a,b,c}}
\end{document}

Best Answer

You should use the \StrRemoveBraces before \StrChar:

\newcommand*{\ExtractFirstChar}[1]{%
    \StrRemoveBraces{#1}[\FirstChar]%
    \StrChar{\FirstChar}{1}[\FirstChar]%
    First char of '#1' is '\FirstChar'\par
}