【问题标题】:Splitting a string in a few substrings without string.split在没有 string.split 的情况下将字符串拆分为几个子字符串
【发布时间】:2018-09-11 05:57:25
【问题描述】:

首先,这是我的代码:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "test1,test2,test3";
        }
    }
}

所以,我想使用子字符串拆分 test1 test2 和 test3,并通过console.writeline() 在控制台中给出。我想为它使用“,”。我不允许使用string.split。你能帮帮我吗?

【问题讨论】:

  • .Split() 有什么问题?
  • 你可以使用正则表达式
  • 最简单的方法是拆分函数,这是预先定义的方法
  • 试试Regex.Split。这不是String.Split :D
  • string 本质上是一个char 的数组。所以你可以很容易地遍历它,并积累字符,直到你到达一个逗号。

标签: c# substring


【解决方案1】:

这里是一个simple solution,使用.IndexOf().Substring() 方法。

string strInput = "test1,test2,test3,";
while (strInput.Length > 0)
{
    int commaIndex = strInput.IndexOf(',');
    if (commaIndex != -1)
    {
        Console.WriteLine(strInput.Substring(0, commaIndex));
        strInput = strInput.Substring(commaIndex + 1);
    }
    else
    {
        Console.WriteLine(strInput);
        strInput = "";
    }
}

在哪里,

String.IndexOf Method 报告第一个从零开始的索引 在此范围内出现指定的 Unicode 字符或字符串 实例。如果字符或字符串不是,该方法返回 -1 在这种情况下发现。 String.Substring Method 将帮助您从给定实例中检索子字符串。

【讨论】:

    【解决方案2】:

    这行得通!

     class Program  
        {  
            static void Main(string[] args)  
            {  
    
                string str = "Test,Data";  
                ArrayList arrayList = new ArrayList();  
                string Temp = "";  
                for (int i = 0; i < str.Length; i++)  
                {  
    
                    if (str[i] != ',')  
                    {  
                        Temp = Temp + str[i];  
                        continue;  
                    }  
    
    
                    arrayList.Add(Temp);  
                    Temp = "";  
                }  
                Console.WriteLine("Enter the no 1 to " + arrayList.Count);  
                int option =Convert.ToInt32(Console.ReadLine());  
                if (option < arrayList.Count && option > 0)  
                {  
                    Console.WriteLine(option+ " position is  = " +arrayList[option - 1]);  
                }  
                else  
                {  
                    Console.WriteLine("Enter only 1 to " + arrayList.Count);  
                }  
                Console.ReadLine();  
            }  
        }  
    

    【讨论】:

    • 为什么不允许?能具体说说吗
    • 它“像”家庭作业。我是一名开发实习生,我的教练给了我这个任务。他要我弄清楚。 string.split 是不允许的,因为它太容易了。 :D
    • 很棒的@Vischi:你希望你的作业问题在stackoverflow上得到解决。干杯人..!但是,如果您不尝试自己解决作业,您将不会学习任何编码。
    【解决方案3】:

    对于非正则表达式的答案:

    string str = "test1,test2,test3";
    string currentSubStr = string.Empty;
    foreach (char c in str)
    {
        if (c == ',')
        {
            Console.WriteLine(currentSubStr);
            currentSubStr = string.Empty;
        }
        else
        {
            currentSubStr = currentSubStr + c;
        }
    }
    

    这适用于非常简单的逗号分隔列表,但如果您处理的是 csv 文件,那么最好使用适当的 csv 解析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 2015-05-26
      相关资源
      最近更新 更多