Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Split string at capital letters

Posted on 2008-10-07
6
Medium Priority
?
4,296 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
[X]
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
6 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 2000 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 60

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 60

Expert Comment

by:Kevin Cross
ID: 22660819
@Roland, welcome by the way.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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