Solved

Split string at capital letters

Posted on 2008-10-07
6
4,228 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now