How to check if a string contains more than 3 empty space in the start and last?

Hi team ,

I have a string A = " 43434343" string B = "2242424 "

How do I check if it contains more than 3 empty spaces in the start AND last ?

Regards,
Gokul

Hi @gokul1904 ,

You could check with the below Expression :

Regex.IsMatch(stringA,"^\s{4,}.*\s{4,}$")

image

Since, you have mentioned more than 3, it would mean 4 and greater number of spaces. However, the string values provided as samples does not match the condition.

2 Likes

HI @gokul1904

Checkout this condition you can pass this in if

System.Text.RegularExpressions.Regex.Matches(InputString,"\s+(?=\b\S)|(?<=\S\b)\s+$").Count>=3

Regards
Sudharsan

Thank you for the reply - can you tell me how do I check spaces before and last separately - not in one reg expression but separate reg expressions - one for empty spaces before and one for spaces in the last

Reg,
G

@gokul1904 ,

Regex For Empty Spaces before /At Start of String :

^\s{4,}

image

Regex For Empty Spaces after / At End of String :

\s{4,}$

image

@gokul1904

for first spaces

System.Text.RegularExpressions.Regex.Matches(InputString,"\s+(?=\b\S)").Count>=3

For last spaces

System.Text.RegularExpressions.Regex.Matches(InputString,"(?<=\S\b)\s+$").Count>=3

Regards
Sudharsan

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