【问题标题】:Parts of a String into an array将字符串的一部分放入数组
【发布时间】:2023-03-16 19:37:01
【问题描述】:

我遇到了以下问题:

我想将字符串的一部分放入一个数组中。到目前为止没有问题(拆分字符串), 但是,我不能使用splitstring,因为它需要我的操作员。

举例说明:
以下字符串:"47-62*5141"
我需要这样:{"47", "-", "62", "*", "5141"}

如果您能给我一个代码示例,我将非常高兴!

【问题讨论】:

  • 请展示你到目前为止尝试过的内容,添加一些代码sn-ps。

标签: c# arrays string split calc


【解决方案1】:
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();
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我编写了一些丑陋的代码。但它有效。

    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();
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

      根据中间存在的单词边界进行分割。

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

      DEMO

      【讨论】:

      • 或匹配单词和非单词字符@"\w+|\W+"
      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2019-08-04
      相关资源
      最近更新 更多