Split string with multiple separators

mjcoon

Well-Known Member
Licensed User
I have a requirement which might not be specific to me... I put the suggestion here even though it might best be met by an extension of StringsEx.dll.

The strings I want to split have been crudely formatted with tabs and spaces to render into columns when using a fixed-width font. Fortunately all columns have a visible character, so when correctly split no element would be null.

Unfortunately the StrSplit(string, "") function will, quite reasonably, treat each white-space character as delineating a new column.

Could we have a version that ignores multiple whitespace? Or a function that does not do a split but contracts all whitespace into, say, a single space? I think it would need a StringBuilder object to do this reasonably efficiently in Basic4PPC code.

Mike.
 

agraham

Expert
Licensed User
Longtime User
There is no intrinsic String method in .NET that would do that so it will have to be synthesised and as they are String operations it is very nearly as efficient when optimised compiled Basic4ppc as when doing it in a library.
B4X:
Sub MergeWhiteSpaces(str)
   str = StrReplace(str, Chr(8), " ")
   Do
      Len1 = StrLength(str)
      str = StrReplace(str, "  ", " ") ' replace two spaces by one
   Loop Until Len1 = StrLength(str)

   Return str
End Sub
You could also use a StringBuilder, iterate the original string with StrAt and append the required characters to the StringBuilder. As the .NET string replace is probably optimised assembly code I would first try using that as I suspect that might be at least as fast unless the loop has to run several times for long lengths of whitespace.
 

mjcoon

Well-Known Member
Licensed User
Hi Andrew, thanks for those suggestions.

The replacing of space-pairs with singles is something I have done with such files in a basic editor like Notepad in times gone by, but when treating larger runs of spaces I used a "binary chop" sort of sequence, halving the number of spaces (from, say, 64) to replace each time and thus reducing the number of repetitions. Since StringBuilder offers an easy way to halve the length of the replacement set of spaces each time I might try an automation of that...

Mike.
 
Top