3

I've got the following Problem:

I want to take parts of a string into an array. So far no problem (splitstring), but, I can't use splitstring because it takes my operators out.

Explained on an example:
Following string: "47-62*5141"
I need it like this: {"47", "-", "62", "*", "5141"}

If you could give me a code example, I would be very pleased!

1
  • 1
    Please show what you have tried so far, add some code snippets. Commented Sep 27, 2015 at 17:21

3 Answers 3

2

Just split according to the word boundary which exist at the middle.

Regex.Split(string, @"(?!^)\b(?!$)");

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

or match word and non-word chars @"\w+|\W+"
0

I've made a little uglycode. But it works.

class Program
{
    static void Main(string[] args)
    {
        var text = "47-62**5141";

        var splittedText = text.SplitAndKeepSeparator('-', '*');

        foreach (var part in splittedText)
        {
            Console.WriteLine(part);
        }
        Console.ReadLine();
    }

}

public static class StringExtensions
{
    public static IEnumerable<string> SplitAndKeepSeparator(this string s, params char[] seperators)
    {
        var parts = s.Split(seperators, StringSplitOptions.None);

        var partIndex = 0;
        var isPart = true;
        var indexInText = 0;
        while (partIndex < parts.Length)
        {
            if (isPart)
            {
                var partToReturn = parts[partIndex];

                if (string.IsNullOrEmpty(partToReturn))
                {
                    partToReturn = s[indexInText].ToString();
                }
                else
                {
                    isPart = false;
                }
                indexInText += partToReturn.Length;
                partIndex++;

                yield return partToReturn;
            }
            else
            {
                var currentSeparator = s[indexInText];
                indexInText++;
                isPart = true;
                yield return currentSeparator.ToString();
            }
        }
    }

Comments

0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] parts = new string[100];

            var text = "47-62*5141";
            int i = 0;

            var splittedText = text.SplitAndKeepSeparator('-', '*', '+', '/');

            foreach (var part in splittedText)
            {
                    parts[i] = part;
                    i++;

            }
            Console.ReadLine();
            Console.WriteLine(parts[0]);
            Console.WriteLine(parts[1]);
            Console.WriteLine(parts[2]);
            Console.WriteLine(parts[3]);
            Console.ReadLine();
        }
    }
}
public static class StringExtensions
{
    public static IEnumerable<string> SplitAndKeepSeparator(this string s, params char[] seperators)
    {
        var parts = s.Split(seperators, StringSplitOptions.None);

        var partIndex = 0;
        var isPart = true;
        var indexInText = 0;
        while (partIndex < parts.Length)
        {
            if (isPart)
            {
                var partToReturn = parts[partIndex];

                if (string.IsNullOrEmpty(partToReturn))
                {
                    partToReturn = s[indexInText].ToString();
                }
                else
                {
                    isPart = false;
                }
                indexInText += partToReturn.Length;
                partIndex++;

                yield return partToReturn;
            }
            else
            {
                var currentSeparator = s[indexInText];
                indexInText++;
                isPart = true;
                yield return currentSeparator.ToString();
            }
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.