?
Solved

How to separate the words and number from a string?

Posted on 2014-10-02
2
Medium Priority
?
144 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
[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
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 1000 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 1000 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

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 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