# Testing for commercial at (@) in a string [duplicate]

I want to test whether a string contains the @ sign. I came up with the following piece of code:

\makeatletter
\def\@testat#1@#2\@nil{%
\def\@cdr{#2}
\ifx\@cdr\@empty #1#2: does not have at\else #1#2: has at\fi}
\def\hasat#1{\@testat#1@\@nil}
\makeatother


Now this works fine if the document is written with \makeatletter:

\makeatletter
\newcommand\test[1]{#1}
\test{\hasat{linear@ set@}}
\hasat{linear@ set@}
\hasat{linear set}


all work fine. But without the \makeatletter, this fails miserably. If instead I define:

\def\@hasat#1{\@testat#1@\@nil\makeatother}
\def\hasat{\makeatletter\@hasat}


then only the \hasat which is not in a \test works for some reason.

Any help welcome!

-

## marked as duplicate by Harish Kumar, egreg, Stefan Kottwitz♦Nov 4 '12 at 23:27

Dammit. Stackexchange now pops up the relevant related question: tex.stackexchange.com/questions/17393/… . Feel free to close this question as a duplicate. –  Michaël Nov 4 '12 at 23:03
Don't define \@cdr as part of your macro; it's already a macro used in the kernel and redefining it can lead to obscure errors. –  egreg Nov 4 '12 at 23:09
Strange, I would not consider this as a duplicate of the other one. If we really think they're "identical", I would say that this one is better and provides a better answer. –  tohecz Nov 5 '12 at 9:59

You can detect if the given string contains a character with the xstring package:

## Code:

\documentclass{article}
\usepackage{xstring}

\newcommand*{\HasAt}[3]{%
\StrPosition{#1}{@}[\Position]
#1 \IfEq{\Position}{0}{#3}{#2}%
}

\begin{document}

\HasAt{ancd@foo.bar}{does}{does not} contains the @ symbol.

\HasAt{ancdfoo.bar}{does}{does not} contains the @ symbol.

\end{document}

-