Sam Gentile
Sam Gentile

Reputation: 1349

C#: How to detect . in string and insert a space after it/How to insert space after n chars?

Suppose I have a long string like 4600airportburlingame150anzablvd.burlingamecalifornia94010. My code is breaking on this string. This is unusual, 99% of entries will not have a period. The CSS in the browser wraps if there are spaces in the string and there isn't any here.

  1. How do I detect the period (".") and insert a space directly after it? Remember 99% of strings will not have a period in them. The code has to detect if it has a period and if so, do the insertion, otherwise not.
  2. If I determine a maximum string length, how do I insert a space at some length?

Upvotes: 1

Views: 8766

Answers (5)

Numan
Numan

Reputation: 3948

you can use string.Replace(".", ". ")

String myText = "4600airportburlingame150anzablvd.burlingamecalifornia94010";
myText = myText.Replace(".", ". ");

Upvotes: -1

Timwi
Timwi

Reputation: 66604

To insert a space after every dot:

var newString = oldString.Replace(".", ". ");

To insert a space after every dot except for the dots that already have a space or dots at the end of the string:

var newString = Regex.Replace(oldString, @"\.(?! |$)", ". ");

To insert a space after every n characters:

var newString = Regex.Replace(oldString, new string('.', n), m => m.Value + " ");

Upvotes: 9

DRapp
DRapp

Reputation: 48169

Although a simple "String.Replace()" would work if you were only concerened with a single period, but what if you had multiple periods... or some already with a period space and others without... would you want to change ". " to ". "? Make this a function and pass a string to be "fixed"... this example uses multiple embedded periods with / without spaces after. The result is a string where ONLY those periods without a following space will have one added to it.

String myText = "4600airpo. rtburl.ingame150.  anzablvd.burlinga.  mecalif.ornia94010"; 
int Dot = 1, LastDot = 0, DotSpace = 0;
while (Dot > 0)
{
   Dot = myText.Substring( LastDot ).IndexOf( "." );
   if (Dot > 0)
   {
      DotSpace = myText.Substring(LastDot).IndexOf(". ");
      if (Dot != DotSpace)
         myText = myText.Substring(0, LastDot + Dot +1) + " " 
                + myText.Substring(LastDot + Dot +1);

      LastDot += Dot +1;
   }
}

Upvotes: 2

Pabuc
Pabuc

Reputation: 5638

Edit: Just edited my post. This will loop till there is no "." in your string. ıf there is none, it will skip it.

String myText = "4600airportburlingame150anzablvd.burlingamecalifornia94010";

while(myText.Contains("."))
{
    int indexOfDot = myText.IndexOf(".");
    myText = myText.SubString(0,indexOfDot) + " " + myText.SubString(indexOfDot + 1);
}

Upvotes: 2

user
user

Reputation: 6947

For 1, try myStr = myStr.Replace(".", ". ");. Do note that this assumes that a period will never be used in any other context (such as, for example, a decimal separator).

For 2, see Pabuc's answer.

Upvotes: 0

Related Questions