How to remove multiple spaces from a string?



How do i remove multiple spaces from a string and replace with a single space?

output: STR=“This is a test.”

assume “.” as white spaces, as i am to able to use white spaces in the post.


Regex.Replace(STR, " {2,}", " ")

ps: please import System.Text.RegularExpressions

How to replace multiple occurrences of a character with just one instance of the same character

Thanks for immediate reply. I am getting the following error.
Main has thrown an exception

Message: parsing “{2}” - Quantifier {x,y} following nothing.

Source: Assign

Exception Type: ArgumentException

System.ArgumentException: parsing “{2}” - Quantifier {x,y} following nothing.
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
at System.Text.RegularExpressions.Regex…ctor(String pattern, RegexOptions options, TimeSpan matchTimeout, Boolean useCache)
at System.Text.RegularExpressions.Regex.Replace(String input, String pattern, String replacement)
at lambda_method(Closure , ActivityContext )
at Microsoft.VisualBasic.Activities.VisualBasicValue1.Execute(CodeActivityContext context) at System.Activities.CodeActivity1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity1 expressionActivity) at System.Activities.InArgument1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate) at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)


looks like your assign(LHS) variable is of type generic please change to string and try.


Got it thanks :slight_smile: Cheers !!!


@santhosh.sanu95 can you please how to import System.Text.RegularExpressions.I am doing through by giving full path like system.Text.RegularExpressions.Regex.Replace(STR, " {2,}", " ") and getting expected output.What is the meaning of {2,} in this expression?


Hi ddpadel,

I m unable to import ‘‘System.Text.RegularExpressions’’ package, Can you please help how can I do so. I have installed and updated every package. But still not able to use ‘Regex.’. Please help. Thanks!


Please ignore. I got it. Thanks!


I got the same error, altough the ‘To’ variable type is String What do I need to do?


i wanted to know like how we can remove dots present in the text file
eg:…clientid:1234…client name:abc
how to extract string like this from the text file.Do anyone have an idea,how to solve this?



I’ve found useful regex for removing additional whitespaces:


It can be used within UiPath activity (.*?) Matches as the pattern (mind string quotation).This regex matches any string ending with only one whitespace (or non-whitespace)