Solved

How to separate the words and number from a string?

Posted on 2014-10-02
2
136 Views
Last Modified: 2014-10-03
I am working on C# and I am required to separate the words and number from a string.  It does not have any delimiter to separate it.

For example the format of string is page12345 and I am required to separet it as string 1 as page and string 2 as 12345.  Can you please guide me how can I achieve this?

Thanks,
D.
0
Comment
Question by:developer2012
2 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 40358300
This will loop over each character in the string, test if it's a digit, and then add it to the appropriate string.

p1a2g3e4
string1 = "page" / string2 = "1234"

1234page
string 1 = "page" / string2 = "1234"

page1234
string1 = "page" / string2 = "1234"


 string mainString;
        string string1 = "";
        string string2 = "";
        
        foreach (char c in mainString.ToCharArray())
        {
            if (!Char.IsDigit(c))
                string1 += c;
            else
                string2 += c;
        }
      

Open in new window

0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40358885
You could also use regular expressions for this task:

e.g.

using System.Text.RegularExpressions;

...

string word;
string number;
string input = "page1234";
MatchCollection matches = Regex.Matches(input, "(?<word>[a-zA-Z]+)|(?<number>[0-9]+)");

foreach (Match m in matches)
{
    if (!string.IsNullOrWhitespace(m.Groups["word"].Value))
    {
        word = m.Groups["word"].Value;
    }
    else if (!string.IsNullOrWhitespace(m.Groups["number"].Value))
    {
        number = m.Groups["number"].Value;
    }
}

Open in new window


I use an OR condition ( | ) to match one of two groups. The "word" group [ (?<word>...) ] matches against one or more ( + ) letters--upper- and lowercase ( [a-zA-Z] ). The "number" group [ (?<number>...) ] matches against one or more ( + ) numeric digits ( [0-9] ). After the matching runs, I just check to see which group actually captured something, and then assign it to the appropriate variable.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

22 Experts available now in Live!

Get 1:1 Help Now