Alexander Stolte Expert Licensed User Longtime User Jul 21, 2020 #1 Hey, i found this code on stackoverflow to remove whitespaces: B4X: Copy st.replaceAll("\\s","") Removing whitespace from strings in Java I have a string like this: mysz = "name=john age=13 year=2001"; I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I stackoverflow.com on B4A i tried this: B4X: Copy Regex.Replace("\\s",New, "") but it is not working.
Hey, i found this code on stackoverflow to remove whitespaces: B4X: Copy st.replaceAll("\\s","") Removing whitespace from strings in Java I have a string like this: mysz = "name=john age=13 year=2001"; I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I stackoverflow.com on B4A i tried this: B4X: Copy Regex.Replace("\\s",New, "") but it is not working.
Erel B4X founder Staff member Licensed User Longtime User Jul 21, 2020 #2 Code: Copy Regex.Replace("\s", New, "") Upvote 0
M Mahares Expert Licensed User Longtime User Jul 21, 2020 #3 The OP may have a reason for using RegEx, but for those of us who get intimidated by RegEx, for simple replacement, I use: Code: Copy str=str.Replace(" ","") instead of: Code: Copy str= Regex.Replace("\s", str, "") Both are equivalent. Upvote 0
The OP may have a reason for using RegEx, but for those of us who get intimidated by RegEx, for simple replacement, I use: Code: Copy str=str.Replace(" ","") instead of: Code: Copy str= Regex.Replace("\s", str, "") Both are equivalent.
Alexander Stolte Expert Licensed User Longtime User Jul 21, 2020 #4 Mahares said: str=str.Replace(" ","") Click to expand... and then i write more than one whitespace and your code wont work. Upvote 0
Mahares said: str=str.Replace(" ","") Click to expand... and then i write more than one whitespace and your code wont work.
Erel B4X founder Staff member Licensed User Longtime User Jul 21, 2020 #5 The difference is that \s will capture: A space character A tab character A carriage return character A new line character A vertical tab character A form feed character (https://www.w3schools.com/jsref/jsref_regexp_whitespace.asp) The str.Replace code will only capture spaces. Upvote 0
The difference is that \s will capture: A space character A tab character A carriage return character A new line character A vertical tab character A form feed character (https://www.w3schools.com/jsref/jsref_regexp_whitespace.asp) The str.Replace code will only capture spaces.
M Mahares Expert Licensed User Longtime User Jul 21, 2020 #6 Alexander Stolte said: and then i write more than one whitespace and your code wont work. Click to expand... I looked at your StackOverflow link and the example was regarding a space character. That is why I referred to it as ' simple replacement' and that is how I based my post. Upvote 0
Alexander Stolte said: and then i write more than one whitespace and your code wont work. Click to expand... I looked at your StackOverflow link and the example was regarding a space character. That is why I referred to it as ' simple replacement' and that is how I based my post.
Erel B4X founder Staff member Licensed User Longtime User Jul 21, 2020 #7 To complete this discussion, it is worth mentioning String.Trim, which removes prefix and suffix whitespace. Code: Copy str.Trim 'can this line do something? No. Strings are immutable. Should be: str = str.Trim Upvote 0
To complete this discussion, it is worth mentioning String.Trim, which removes prefix and suffix whitespace. Code: Copy str.Trim 'can this line do something? No. Strings are immutable. Should be: str = str.Trim