Solved

Split string at capital letters

Posted on 2008-10-07
6
4,257 Views
Last Modified: 2013-12-17
I have a function which currently splits the country name out like this:

            // delimited string
            string CountryFull = (string)txtValue;
            // separate individual items
            string[] CountrySplit = CountryFull.Split(new char[] { ';' });
            // combine array elements with a new separator
            string CountryDisplay = String.Join("", CountrySplit, 0, 1);
            [insert line in here to add space?]
            return CountryDisplay;

So for 'CountryName;State' it returns 'CountryName'. What I need it to do is return 'Country Name' ie. insert a space before all capital letters after the first capital letter.

Any feedback much appreciated.
0
Comment
Question by:arisbey
6 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 22660496
Mmmm... it seems simple. Try this;
string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int k = 0; (k 
            <= (Characters.Length - 1)); k++) {
    string sLetter = characters.Substring(k, 1);
    original_string = original_string.Replace(sLetter, (" " + sLetter));
}
original_string = original_string.Trim();

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22660812
Nice solution, Roland.
@arisbey, Roland answer should work for you great.  Just remember to change Characters to characters or vice versa or will not compile; however, once you do it works like a champ.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22660819
@Roland, welcome by the way.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 18

Expert Comment

by:Priest04
ID: 22661031
Well, that wouldn't to be the best solution. What about non ASCI letters?

What I dont understand is why are blank spaces left out? This is not a good programming practice.

In this example

Côte d'Ivoire

you cant do much, there are also some countries that have 'and' in their names (like Trinidad and Tobago), so you wont be able to separate and with other two words.

As to add a space before the capital letter, try this method, its not dependant on the standard (ASCII) letters only, but handles Unicode letters too (ex Cyrilic letters)

Goran
string GetCountryname(string countryName)
{
    int i = 1;
 
    while (i < countryName.Length)
    {
        if (Char.IsUpper(countryName[i]))
        {
            countryName = countryName.Insert(i, " ");
            i++;
        }
 
        i++;
    }
 
    return countryName;
}

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22661040
sorry its ASCII.
0
 

Author Closing Comment

by:arisbey
ID: 31503883
I've no idea whether this is the best way of doing it, but it works a treat! Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question