Replacing Slashes In a String

HimeAnator

Member
Licensed User
Longtime User
I have a text box that the user types in name a file but if the user enters "/" or a "\" the app crashes. So what would be the best way that I can check for them and replace them or block them?
 

sorex

Expert
Licensed User
Longtime User
B4X:
t=t.replace("/","")

not sure how java deals with special chars, might need to escape them like "\/" or "\\"
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Just for the record, as I bumped into the issue today. I needed to replace / by \ to make a path and found this thread.

Escaping did not work, replace never saw "/".

Finally, this worked :
B4X:
t.Replace(Chr(47), "\")

I know the topic is old, but I post to avoid others to waste time on a non working solution.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Just for the record, as I bumped into the issue today. I needed to replace / by \ to make a path and found this thread.

Escaping did not work, replace never saw "/".

Finally, this worked :
B4X:
t.Replace(Chr(47), "\")

I know the topic is old, but I post to avoid others to waste time on a non working solution.
This code is identical to:
B4X:
t.Replace("/", "\")
(You don't need escape the slash).

Test it with:
B4X:
Dim t As String = "a/b/c"
Log(t.Replace("/", "\"))
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Indeed on the surface both the variable and the literal are the same thing.

However, just tested again, literal "/" does not work, whereas chr(47) works.

I suspect they are not parsed the same way.
 
Upvote 0
Top