How to remove multiple spaces from a string?

Hi,

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

eg:
STR=“This…is…a…test.”
output: STR=“This is a test.”

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

1 Like

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

ps: please import System.Text.RegularExpressions

11 Likes

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.

1 Like

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?

1 Like

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!

1 Like

Please ignore. I got it. Thanks!

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

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

Thanks.

I’ve found useful regex for removing additional whitespaces:

([^\s*]+\s|\S)

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)

1 Like