\documentclass{article}
\usepackage{tabularx}
\makeatletter
\newcommand*{\tocdotfill}{%
\leaders\hbox{%
$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$%
}\hfill\kern\z@
}
\makeatother
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{lXr}
A & Hello world \dotfill & 1\\
B & Foobar \tocdotfill & 2\\
\end{tabularx}
\end{document}

The important issue is that a skip specification is removed at the end of a table cell via \unskip. \dotfill has added a \kern\z@ at the end to prevent this.
Macro \tocdotfill uses the same code as \dottedtocline, which is used in the table of contents. Also \kern\z@ is appended to prevent from "unskipping". Also other things like \null, an empty \hbox, can be used for this purpose.
\documentclass{...}\usepackage{....}\begin{document}...\end{document}in order to show us what you have tried. If possible, it should compile and have the minimum amount of code needed to illustrate your problem. This makes it much easier for people to help you - and much more likely that they will! – May 28 '15 at 08:28