Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Split string at capital letters

Posted on 2008-10-07
6
4,265 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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