Solved

How to separate the words and number from a string?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 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