2

How do I convert "ThisIsMyTestString" into "This Is My Test String" using C#?

Is there a fast way to do it?

I've been thinking of a pseudo code but it's complicated and ugly:

String s = "ThisIsMyTestString";

List<String> strList = new List<String>();
for(int i=0; i < str->Length ; i++)
{
   String tmp = "";
   if (Char.IsUpper(str[i]))
   {
     tmp += str[i];
     i++;
   }

   while (Char::IsLower(str[i]))
   {
     tmp += str[i];
     i++;
   }

   strList .Add(tmp);
}

String tmp2 = "";
for (uint i=0 ; i<strList.Count(); i++)
{
  tmp2 += strList[i] + " ";
}
5
  • Very similar question here: stackoverflow.com/questions/291804/… Commented Jul 15, 2013 at 15:18
  • personally I'd split anytime you find a char that is < 97
    – Sayse
    Commented Jul 15, 2013 at 15:18
  • 3
    What is the output of the code you wrote? Saying "its so ugly I am not even trying to fix it" indicates your not willing to go to the effort to debug your own code. Commented Jul 15, 2013 at 15:19
  • I see only one string in "This Is My Test String". Are you sure you want to separate one string in multiple strings? Commented Jul 15, 2013 at 15:20
  • To author: Why you think that this way is ugly way? Of course, there is better solution with regexes, but ... That's nice too. Of course you must use StringBuilder for tmp variable, must write this code more nice-styled Commented Jul 15, 2013 at 15:20

4 Answers 4

5

You can use Regex as outlined here:

Regular expression, split string by capital letter but ignore TLA

Your regex: "((?<=[a-z])[A-Z]|A-Z)"

Find and replace with " $1"

string splitString = Replace("ThisIsMyTestString", "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1")

Here (?<=...) is a "positive lookbehind, a regex that should precede the match. In this case the lookbehind is "characters 'a' through 'z'" (?=...) is a similar construct with lookahead, where the match has to be followed by regex-described string. In this case the lookahead is "characters 'a' through 'z'" In both cases the final match contains one character "A" through "Z" followed by 'a'-'z' OR one character 'a' through 'z' followed by a capital letter. Replacing these matches puts a space between the capital and lowercase letters

6
  • +1 regex is nice solution here Commented Jul 15, 2013 at 15:26
  • thx alot. but can some one explain me what this does ???
    – Dardar
    Commented Jul 15, 2013 at 15:54
  • The regex looks for characters from A to Z preceded by characters a through z and for each instance found it replaces the instance with a space, followed by the character found Commented Jul 15, 2013 at 16:34
  • See updates to the answer Commented Jul 15, 2013 at 16:39
  • thank you verym much! that helped alot!
    – Dardar
    Commented Jul 16, 2013 at 6:35
3

Not best code, but it works

String.Join("", s.Select(c => Char.IsUpper(c) ? " " + c : c.ToString())).Trim()
2
  • on a long string this may cause memory spike Commented Jul 15, 2013 at 15:21
  • @StenPetrov completely agree with you here Commented Jul 15, 2013 at 15:21
1

lazyberezovsky beat me with a much simpler solution... but this creates less garbage so I won't delete it.

static void Main(string[] args)
{
    Console.WriteLine(SplitByCase("ThisIsMyString"));
    Console.ReadLine();
}

static string SplitByCase(string str, bool upper = true)
{
    return String.Join(" ", SplitIntoWords(str, c => Char.IsUpper(c)));
}

static IEnumerable<String> SplitIntoWords(string str, Func<char, bool> splitter)
{
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < str.Length; i++)
    {
        sb.Append(str[i]);
        if (i + 1 == str.Length || splitter(str[i + 1]))
        {
            yield return sb.ToString();
            sb.Clear();
        }
    }
}
0

This will do it for that string:

            String s = "ThisIsMyTestString";
            StringBuilder result = new StringBuilder();
            result.Append(s[0]);
            for (int i = 1; i < s.Length; i++)
            {
                if (char.IsUpper(s[i]) )
                    result.Append(' ');
                result.Append(s[i]);
            }
            s = result.ToString();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.