How can I standardize spacing between sequences of numbers in strings?

Hi!

I’d like to know how I can use the String Manipulation node in order to correct the spacing between numbers in strings in a database.

There are lines that start with spaces and that have more than one space between the numbers. Likewise, there are lines that have only one of the aforementioned problems. Below is an example:

Image

You can see that Row3 starts with a space and that Row7 and Row9 have additional spaces between some terms.

The goal is to standardize so that there is no space at the beginning of the string and that there is only one space between each sequence of numbers on each line.

Is there some kind of argument I can use to define this function?

Welcome to the forum @MarconyxD,

In the String Manipulation node, you can use the strip or stripStart function to remove whitespace characters from the beginning of a string.
e.g. strip($columnName$ or stripStart($columnName$)

In a separate node, you can use a replace function to replace double spaces with a single space.
e.g. replace($columnName$," "," ")
There are 2 spaces in between the first pair of quotes.

If you want to use a single String Manipulation node, you can combine the above into something like this:
replace(strip($column1$)," "," ")
again, there are 2 spaces between the first pair of quotes.

5 Likes

Thanks for the welcome, @elsamuel.

The solution worked perfectly. Using your explanation as a base, I was able to set up a more complex String Manipulation to also recognize 3, 4 and 5 spaces between numbers, not just 2.

replace(replace(replace(replace(strip($Text$)," "," ")," "," ")," "," ")," "," ")

I believe the innermost parentheses is executed first, right? So I started from the larger number. If you started from the smallest, you ran the risk of “losing” some spaces (in a case of 4 spaces, if you delete 2 first, the quantity would be updated to 2 and the loop to find 4 would not find these remaining 2, for example). Starting from the highest number of spaces and going to the lowest this problem doesn’t happen and all excess spaces are excluded, as long as it’s not too big haha

It really helped me a lot and I really appreciate it!

2 Likes

Hello @MarconyxD and @elsamuel,

there is removeDuplicates() function in String Manipulation node that “Replaces all occurrences of two or more spaces with a single space character” so there is no need for nested expressions :wink:

Br,
Ivan

6 Likes

Hello @ipazin,

I tested your solution together with strip() to remove spaces at the beginning of the string and it also worked perfectly, generating a less complex code that solves the problem in the same way.

removeDuplicates(strip($columnName$))

Thanks for the sugestion!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.