1

Possible Duplicate:
Regular expression, split string by capital letter but ignore TLA

hello everyone, in c# if i have a string that is a sentence that contain upper case Letters how can i split the words?

for example:

string a = "HelloWorld"

and i need

b[0] = "Hello";
b[1]= "world";
Community
  • 1
  • 1
yonatan
  • 2,133
  • 3
  • 15
  • 8
  • 4
    See http://stackoverflow.com/questions/1097901/regular-expression-split-string-by-capital-letter-but-ignore-tla – Zaki Dec 12 '10 at 15:06
  • "World" or "world"? if "world" then use str.ToLower(); – Javed Akram Dec 12 '10 at 15:30
  • Hey buddies, why did you close the question? These are not the same Question. – Jahan Zinedine Dec 12 '10 at 15:41
  • Here are two more solutions by LINQ. IEnumerable enumerable = preString.Select( c => Char.IsUpper(c) ? " " + c.ToString(): c.ToString()); MessageBox.Show(string.Concat(enumerable.ToArray())); IEnumerable selectMany = preString.SelectMany(o => Char.IsUpper(o) ? o.ToString() : " " + o.ToString()); MessageBox.Show(new string(selectMany.ToArray())); – Jahan Zinedine Dec 12 '10 at 17:31

1 Answers1

4

Try:

String preString = "HelloWorld";
StringBuilder sb = new StringBuilder();

foreach (char c in preString)
{
    if (Char.IsUpper(C))
        sb.Append(' ');
    sb.Append(C);
}

string result = sb.ToString();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Tee Wu
  • 569
  • 2
  • 9