Solved

How to separate the words and number from a string?

Posted on 2014-10-02
2
138 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 40

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 75

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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