Remove certain characters from a string/RichTextBox?

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,

My VB is very rusty I'm afraid! What would be the most efficient way to
remove the following ASCII characters from a string?

à è ì ò ù À È Ì Ò Ù
á é í ó ú ý Á É Í Ó Ú Ý
â ê î ô û Â Ê Î Ô Û
ã ñ õ Ã Ñ Õ
ä ë ï ö ü ÿ Ä Ë Ï Ö Ü Y o O æ Æ
¡ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± º ¹ ² ³ ´ · » ¼ ½ ¾ ¿
Å å Ç ç Ð Ø ø ð µ × Þ þ ß . -

I want to load the contents of a file (which contains normal numbers &
letters aswell as the ones above) and then remove each occurance of each
unwanted character.

Preferably I want to show the contents of the file in the RichTextBox and
then press a command button to remove the ASCII characters and then update
the RichTextBox.

I just need some clever person to tell me how I can remove these horrible
characters! I'm guessing one way is setting up an array with these
characters in, then looping through the RichTextBox to seek out the
occurances of each character? If only I knew how to do this!

Thanks
Paul
 
Hi

Maybe i'm not very clever but what about replace function?:> it can't
be?

Imports System.Text.RegularExpressions
..
..
Regex.Replace(value, "old char", "new char"))


what about it??

Mrozu

Paul napisal(a):
 
Hi

Maybe i'm not very clever but what about replace function?:> it can't
be?

Imports System.Text.RegularExpressions
..
..
Regex.Replace(value, "old char", "new char"))


what about it??

Mrozu

Paul napisal(a):
 
Yes that works but it looks very bad.

I was hoping to set up an array and loop through the RichTextBox but thanks
anyway!

RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "à", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "è", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ì", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ò", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ù", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "À", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "È", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ì", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ò", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "(Ù)", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "á", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "é", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "í", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ó", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ú", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ý", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Á", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "É", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Í", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ó", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ú", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "(Ý)", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "â", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ê", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "î", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ô", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "û", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Â", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ê", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Î", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ô", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Û", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ã", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "ñ", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "õ", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ã", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Ñ", " ")
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Õ", " ")


Paul


Hi

Maybe i'm not very clever but what about replace function?:> it can't
be?

Imports System.Text.RegularExpressions
..
..
Regex.Replace(value, "old char", "new char"))


what about it??

Mrozu

Paul napisal(a):
 
If you have the list of the unwanted characters in a file, you could
read in the file line by line, split the line by spaces, and loop
through doing a replace that way. Something like this

Dim fs As New FileStream(YourUnwantedCharFileLoc, FileMode.Open,
FileAccess.Read)
Dim g As New StreamReader(fs)
dim numchars as integer
'make sure you start at the beginning
g.BaseStream.Seek(0, SeekOrigin.Begin)
Dim ArrayLine() As char
'while you are not at the end of the file
While g.Peek() > -1
'split into chars
ArrayLine = Split(g.ReadLine(), " ")
For numchars = 0 to ArrayLine.Length
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text,
ArrayLine(numchars), " ")
next
End While
 
Paul said:
Yes that works but it looks very bad.

I was hoping to set up an array and loop through the RichTextBox but thanks
anyway!

You can replace multiple characters with a single RegEx.Replace() call.
Supply a list of all the characters you want to replace, surrounded by
square "[", "]" brackets.

the statement:

Regex.Replace("abcdefg", "[aceg]", " ")

returns the string " b d f ".
 
Thanks but I get a error before I'm able to compile...

Value of type '1-dimensional array of String' cannot be converted to
'1-dimensional array of Char' because 'String' is not derived from 'Char'.

It occurs on this line...
ArrayLine = Split(g.ReadLine(), " ")

Any ideas what that could mean?

Paul
 
Thanks Douglas, I've incorporated this into my project, had no idea about
the square brackets!"

Paul


Douglas Richard said:
Yes that works but it looks very bad.

I was hoping to set up an array and loop through the RichTextBox but
thanks
anyway!

You can replace multiple characters with a single RegEx.Replace() call.
Supply a list of all the characters you want to replace, surrounded by
square "[", "]" brackets.

the statement:

Regex.Replace("abcdefg", "[aceg]", " ")

returns the string " b d f ".
 
Back
Top