How to replace whitespace

Hello guys i have string in which i want to replace whitespace in between them with “*”

   1 Google        USD          -4,800.54              -4,800.54

Output Result → 1 GoogleUSD-4,800.54*-4,800.54

This whitespace is not fixed It will come like the below also

   1 Google   USD    -4,800.54     -4,800.54

So i need a solution for any whitespace in between them

1 Like

HI @Gokul_Murali

You can use Regex Replace to replace whitespaces dynamically (regardless of the size of space)

System.Text.RegularExpressions.Regex.Replace("1 Google        USD          -4,800.54              -4,800.54".Trim, "\s+","*")

The output:
image

The syntax is:

System.Text.RegularExpressions.Regex.Replace(input, pattern, replacement)

If your input has whitespaces before/after it, use Trim:

System.Text.RegularExpressions.Regex.Replace(input.Trim, "\s+","*")

If this solves your query, Do mark it as a solution.
Happy Automation :star_struck:

Hi @Gokul_Murali

Use the below syntax:
Input = " 1 Google USD -4,800.54 -4,800.54"
Output = System.Text.RegularExpressions.Regex.Replace(Input.TrimStart(),"[\s]+","*")

Hope it helps!!

While other mentioned comments will work

Here is another approach


String.Join("*", "1 Google        USD          -4,800.54              -4,800.54".Split({" "c}, StringSplitOptions.RemoveEmptyEntries))