stoic
stoic

Reputation: 4830

Split string on uppercase only if next character is lowercase

I have found numerous examples on how to split a string on uppercase, example:

"MyNameIsRob" returns "My Name Is Rob"

My scenario is a bit differnet through....

I would like to accomplish the following:

"MyFavouriteChocIsDARKChocalate" should return "My Favourite Choc Is DARK Chocalate"

The only way I can think of doing this, is to only split the string on upperacase if the next character is lowercase.

Any idea on how to achieve this?

Upvotes: 5

Views: 2349

Answers (4)

Brocco
Brocco

Reputation: 64893

Without regex, you could use this:

public static string SplitOnCaps(string s)

http://dotnetfiddle.net/qQIIgX :

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    public static void Main()
    {
        /*"MyFavouriteChocIsDARKChocalate" should return "My Favourite Choc Is DARK Chocalate"*/
        var input = "MyFavouriteChocIsDARKChocalate";
        var split = SplitOnCaps(input);
        Console.WriteLine(input + " --> " + split);
        var match = "My Favourite Choc Is DARK Chocalate";

        Console.WriteLine(split == match ? "Match" : "No Match");       
    }

    public static string SplitOnCaps(string s)
    {
        var splits = new List<int>();

        var chars = s.ToCharArray();

        for(var i=1; i<chars.Length-1; i++)
        {
            if (IsCapital(chars[i]) && !IsCapital(chars[i+1]) ||
               IsCapital(chars[i]) && !IsCapital(chars[i-1]))
            {
                splits.Add(i);
            }
        }

        var sb = new StringBuilder();

        var lastSplit = 0;
        foreach(var split in splits)
        {
            sb.Append(s.Substring(lastSplit, split - lastSplit) + " ");
            lastSplit = split;
        }
        sb.Append(s.Substring(lastSplit));

        return sb.ToString();
    }
    public static bool IsCapital(char c)
    {
        var i = (int)c;
        return i>=65 && i<=90;
    }
}

Note: I am not fluent in Regex

Upvotes: 2

Dmitry
Dmitry

Reputation: 14059

Maybe classic approach will fit your need:

private static string[] SplitSpecial(string s)
{
    List<string> tmp = new List<string>();
    int lastindex = 0;
    for (int i = 1; i < s.Length; i++)
        if (Char.IsUpper(s[i]) && (Char.IsLower(s[i - 1]) || (i < s.Length - 1 && Char.IsLower(s[i + 1]))))
        {
            if (i > lastindex)
                tmp.Add(s.Substring(lastindex, i - lastindex));
            lastindex = i;
        }
    tmp.Add(s.Substring(lastindex, s.Length - lastindex));
    return tmp.ToArray();
}

To make a single string you can String.Join the resulting array:

string singleStr = String.Join(" ", SplitSpecial("MyFavouriteChocIsDARKChocalate"));

Upvotes: 1

Gabriel GM
Gabriel GM

Reputation: 6639

Classic approach + using stringBuilder (more efficient if you work with long strings) :

    string str = "MyFavouriteChocIsDARKChocalate";
    System.Text.StringBuilder output = new System.Text.StringBuilder(str.Substring(0,1));

    for (int i = 1; i < str.Length; i++)
    {
        if (Char.IsUpper(str[i]) && (!char.IsUpper(str[i-1]) || (i+1 < str.Length && char.IsLower(str[i+1]))))
        {
            output.Append(" " + str[i]);
        }
        else
        {
            output.Append(str[i]);
        }
    }
    string result = output.ToString();

Upvotes: 1

Anthony Chu
Anthony Chu

Reputation: 37520

You can do a Regex replace with lookahead and lookbehind to find uppercase with a lowercase before or after it...

var input = "MyFavouriteChocIsDARKChocalate";
var output = Regex.Replace(input, "(((?<!^)[A-Z](?=[a-z]))|((?<=[a-z])[A-Z]))", " $1");
Console.WriteLine(output);

http://dotnetfiddle.net/cIM6QG

Upvotes: 6

Related Questions