Solved

Split string at capital letters

Posted on 2008-10-07
6
4,278 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 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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