I want to remove the special characters as well as extra spaces

Hi,

I have one text file, which have lot of data. I want to remove the extra spaces and special characters from it.

I have use, Regex.Replace( word1, " {2,}", " ") But there is no change in output.

Please give suggestions.

Thanks.
@ClaytonM @palindrome @dmdixon75

1 Like

Hi,
It would be worth to give sample of text file

Cheers

Hi,

Please find the screen shot of Text file.

Please use replace function for whichever characters you need to replace.

String1.Trim.Replace(“$”,“”).Replace(“%”,“”).Replace(“#”,“”)

Hi,

But it will not remove the extra spaces.

HI,

I tried with .trim, But no changes in output. Spaces are same.

Hi @Jayesh_678,
Right, it is hard to validate regex vs. screenshot :frowning:
In principle your statement is correct and works so the problems lies in the text file, I guess.

Cheers

EDIT: I mean that spaces are likely not spaces (ASCII 32)

Hi @Vivek_Arunagiri,
Trim method will remove only trailing/leading spaces but not spaces in between string, right?

Cheers

Yes. You are correct :smiley:

1 Like

So, we can not remove that?

Actually because of that spaces and special characters I can not split the exact value from all documents.

You could try Regex.Replace( word1, “\s{2,}”, " ")
And check in HEX editor if spaces are spaces

Did you tried Regex.Replace(text, “\s+”," ")

Hey,

It works.

Is there any syntax, so we can keep A-Z and 0-9 only?

Hey @palindrome ,

It works.

Is there any syntax, so we can keep A-Z and 0-9 only?

you can try this to replace all but letters and digits.
Regex.Replace(text, “[^\w\d]+”, “”)

3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.