Filtering unwanted characters out

I need to filter unwanted characters out of from a string like this:

[code] Dim text As String = “hello world!”
Dim allowedChars As String = “1234567890 abcdefghijklmno?!$€¥-_+,;()[]{}«»/\*.<>”
Dim finalMessage As String

Dim arrayedChars(-1) As String = allowedChars.Split("")

For Each char As String In text.Split("")
If arrayedChars.IndexOf(char) > -1 Then
finalMessage = finalMessage + char
End If
Next

msgBox finalMessage[/code]

This works fine, but I wonder if Xojo already has a built-in feature to do the same in a more efficient way.

Literally the first search result.

String.ReplaceAll

Tim, how would ReplaceAll remove everything BUT the list of valid characters?

[code] Dim text As String = "hello world! "
Dim allowedChars As String = “1234567890 abcdefghijklmno?!$-_+,;()[]{}/\*.<>”
Dim finalMessage As String = text

for i as integer = 32 to 5000 //Unicode code points
if instr(allowedChars,chr(i)) = 0 then
finalMessage = ReplaceAll(finalMessage,chr(i),"")
end if
next

msgbox finalMessage
[/code]

Note that since allowedChars does not contain H or W and r, the result is “ello old!”

[quote=123561:@Tim Parnell]

Literally the first search result.

String.ReplaceAll[/quote]

ReplaceAll could assist me with replacing undesired characters like “á” to “a” etc, yes, one by one.

However I find the .ConvertEncoding(Encodings.ASCII) much better.

Karma is a b . . . c h :stuck_out_tongue:

Thanks Michel, looks beautiful to me :slight_smile: I will do some tests

[quote=123568:@Walter Sander]ReplaceAll could assist me with replacing undesired characters like “á” to “a” etc, yes, one by one.

However I find the .ConvertEncoding(Encodings.ASCII) much better.

Karma is a b . . . c h :p[/quote]

These are not the same at all. ConvertEncoding is used to make a string compatible with a particular environment, and you can remove accents with it.

But from you first code, it seemed you wanted to allow some characters and not others. I added accented characters to my example to take into account higher Unicode code points.

ReplaceAll should be faster than looking at every character, especially on long strings. Hello World is a bit short…

I am dealing with short strings yes, less than 1024 bytes, thus the performance shall not be an issue. Jeremy is probably replying part of my initial post I deleted. Anyway …

I am now trying to revert “bc” I send from a Javascript websocket client (a), to my PHP websockets server (b), and this server is echoing back to all clients. Xojo is one of those clients (b).

This is what I get:

The Javascript client shows correct data but Xojo doesn’t :frowning:

So if I understand right, what you should get through Xojo should be “bc”, and instead you get “bc” ?

How are you getting the string you display ? From an HTTPSocket ? You may simply need to

DeFineEncoding(string,Encodings.UTF8) the string you get. See http://documentation.xojo.com/index.php/DefineEncoding

Michel, that solved the issue!
I’ve had tested before; probably did something wrong. Thanks