This is a little code function that I developed to remove ‘extra’ spaces from a string. I.e. removing any groups of two or more spaces.
Basic theory is to replace all pairs of spaces with a single space, and keep doing this until no pairs of spaces exist in the string (checking for pairs still returns true for three or more spaces as these contain a pair).
public static string RemoveExtraSpaces(string str) { if (str.IndexOf(" ") == -1) { return str; } else { return RemoveExtraSpaces(str.Replace(" "," ")); } }
while(str.contains(” “))
str = str.replace(” “, ” “);
Hi, yes you’re correct, I was trying to illustrate an example using recursion, should have been clearer in the explanation. Thanks