Parsing leading hardspaces

Classic TeX doesn't really have a "hard space", in the sense of say U+00a0 (nbsp in html) ~ is a macro that expands to some commands that make a space and prevent linebreaking, but that isn't really quite the same thing. Since ~ is a macro the easiest way to make it go away is to define it to expand to nothing. I suggest some code below which produces

[Thisisatestwith2hardspaces(lead+everywhere)]

you should be able to combine it with code to remove .

\documentclass{article}

\makeatletter

\def\remtilde#1{{%
\let~\@empty
\protected@xdef\thestring{#1}%
\typeout{[\thestring]}}}


\remtilde{~~This~~is~~a~~test~~with~~2~~hardspaces~~(lead+everywhere)}

\stop

If it is just to remove spaces and if you want to save time, why not changing their catcode just when reading the argument? No loop, no recursion and above all, no \edef and no global to store the argument in \thestring

\documentclass{article}
\usepackage[T1]{fontenc}
\makeatletter
\newcommand\noblanksF[1][v]{%
    \edef\opt@arg{\expandafter\@car\detokenize{#1}\@nil}%
    \begingroup
        \ifnum\catcode`\~=\active\catcode`\~9 \fi
        \catcode32=9
        \afterassignment\noblankF@i
        \def\thestring}

\newcommand\noblankF@i{%
        \expandafter
    \endgroup
    \expandafter\def\expandafter\thestring\expandafter{\thestring}%
    \if\string v\opt@arg\expandafter\thestring\fi}
\makeatother
\def\bl{\vrule height1ex width1ex depth0pt }
\begin{document}
\parindent 0pt
Testing noblanks: \par
\bl\noblanksF{This is a test with 0 leading spaces}\bl\par
\bl\noblanksF{ This is a test with 1 leading space}\bl\par
\bl\noblanksF{  This is a test with 2 leading spaces}\bl\par
\bl\noblanksF{  This  is  a  test  with  2  spaces  (lead+everywhere)}\bl\par
FROM HERE OUT, RESULTS LEAVE ONE LEADING SPACE:\par
\bl\noblanksF{~This is a test with 1 leading hardspace}\bl\par
\bl\noblanksF{~~This is a test with 2 leading hardspaces}\bl\par
\bl\noblanksF{~~~This is a test with 3 leading hardspaces}\bl\par
\bl\noblanksF{~~~~This is a test with 4 leading hardspaces}\bl\par
\bl\noblanksF{~~This~~is~~a~~test~~with~~hardspaces~~~and~maths$1+1=2$!}\bl\par
\catcode`\~12 % "~" become a "other" char
HERE, "~" IS A NORMAL CHAR:\par
\bl\noblanksF{~~This~~is~~a~~test~~with~NO~hardspaces~~~and~maths$1+1=2$!}\bl
\end{document}