clear string

Andy75

Member
Joined
May 29, 2020
Messages
18
Programming Experience
1-3
Hi all,
how do I extract only numbers and sign '.' from a string like this in C#?

String input:
<b><b><font color =\"red\">120</font></b></b>.953.25.123.934│$$$$$$$"

Result output:
120.953.25.123.934

thank you

A.
 
Solution
I know you have to use regex
You don't have to, but you can..

C#:
//regex
var output = Regex.Replace(input, "[^0-9.]", "");

There are many ways to skin the cat

C#:
//no regex, but LINQ
var output = string.Concat(input.Where(c => c is '.' or (>= '0' and <= '9')));

//or in really old money
var sb = new StringBuilder();
var h = "0123456789.".ToHashSet();
foreach(var c in input) if(h.Contains(c)) sb.Append(c);
var output = sb.ToString();
You would parse through the string and ignore the characters you don't want and only accept the characters that you do want.

Have you tried that? Show us what you've tried so far.

What specific problem are you running into?

Or is there an unstated requirement that you want the filtering to look like a one-liner, or as short as possible or have the code as short a possible?
 
Moving to C# General since this doesn't look like a WinForms question.
 
It's winforms.
It's a string that I pass to Crystal Reports, but first I have to filter it to process the data via code.
I haven't tried anything, I know you have to use regex, but I'm not able.
Otherwise I would not write here.

thank you
 
You don't have to use a regex. A simple loop would work. Here's pseudo-code:
C#:
output = ""
for each character in input
    if character is a number or a period then
        append character to output

That pseudo-code should work until you get something that looks like <p style="width:600;">123</p> would result in 600123.
 
If your request is actually to parse HTML and then remove the HTML tags, the common wisdom is to not try to parse HTML with regular expressions. You can get part of the way with regular expressions if the HTML is highly controlled. To handle the general case, use the HTML Agility Pack instead.
 
I know you have to use regex
You don't have to, but you can..

C#:
//regex
var output = Regex.Replace(input, "[^0-9.]", "");

There are many ways to skin the cat

C#:
//no regex, but LINQ
var output = string.Concat(input.Where(c => c is '.' or (>= '0' and <= '9')));

//or in really old money
var sb = new StringBuilder();
var h = "0123456789.".ToHashSet();
foreach(var c in input) if(h.Contains(c)) sb.Append(c);
var output = sb.ToString();
 
Solution
Back
Top Bottom