I have a document which is similar to the MWE below:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

% toc dots
\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother

\begin{document}

\tableofcontents

\section{Test section one \hfill ID:100}
\section{Test section two \hfill ID:200}
\section{Test section three \hfill ID:300}
\section{Test section four \hfill ID:400}
\end{document}


Which yields:

I only want the hfill to be present in the section titles. The problem I have is that the table of contents also displays the hfill, is there a way to get the ToC to ignore the hfill?

The optional argument of \section could be used to provide a different section title for the table of contents. But here, it would require to repeat the title text unnecessarily. Instead, the following example defines a macro \IgnoreToc. When the section title is set, the macro just passes its argument through. The macro is redefined to ignore its argument in the table of contents:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

% toc dots
\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother

\DeclareRobustCommand*{\IgnoreToc}[1]{#1}

\begin{document}

\begingroup
\renewcommand*{\IgnoreToc}[1]{}%
\tableofcontents
\endgroup

\section{Test section one \IgnoreToc\hfill ID:100}
\section{Test section two \IgnoreToc\hfill ID:200}
\section{Test section three \IgnoreToc\hfill ID:300}
\section{Test section four \IgnoreToc\hfill ID:400}
\end{document}