Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中拆分不带';t使用String.Split()_C#_String - Fatal编程技术网

在c#中拆分不带';t使用String.Split()

在c#中拆分不带';t使用String.Split(),c#,string,C#,String,我看到这样一个问题:给定一个字符串“smith;rodgers;McCalne”,如何生成一个集合。答案是使用String.Split 如果我们没有内置Split(),您会怎么做 更新: 我承认编写拆分函数相当容易。下面是我会写的。使用IndexOf循环字符串并使用Substring提取 string s = "smith;rodgers;McCalne"; string seperator = ";"; int currentPosition = 0; int lastPosition =

我看到这样一个问题:给定一个字符串“smith;rodgers;McCalne”,如何生成一个集合。答案是使用String.Split

如果我们没有内置Split(),您会怎么做

更新:

我承认编写拆分函数相当容易。下面是我会写的。使用IndexOf循环字符串并使用Substring提取

string s = "smith;rodgers;McCalne";

string seperator = ";";
int currentPosition = 0;
int lastPosition = 0;

List<string> values = new List<string>();

do
{
    currentPosition = s.IndexOf(seperator, currentPosition + 1);
    if (currentPosition == -1)
        currentPosition = s.Length;

    values.Add(s.Substring(lastPosition, currentPosition - lastPosition));

    lastPosition = currentPosition+1;

} while (currentPosition < s.Length);
string s=“smith;rodgers;McCalne”;
字符串分隔符=“;”;
int currentPosition=0;
int lastPosition=0;
列表值=新列表();
做
{
currentPosition=s.IndexOf(分离器,currentPosition+1);
如果(当前位置==-1)
当前位置=s.长度;
添加(s.Substring(lastPosition,currentPosition-lastPosition));
lastPosition=currentPosition+1;
}while(当前位置
我看了一眼SSCLI实现,它与上面的类似,只是它处理了更多的用例,并且在进行子字符串提取之前,它使用了一种不安全的方法来确定分隔符的索引

其他人提出了以下建议

  • 使用迭代器块的扩展方法
  • 正则表达式建议(未实施)
  • Linq聚合法
  • 是这个吗?

    Regex


    或者只是子字符串。这就是Split在内部所做的

    编写自己的
    Split
    等价物相当简单

    下面是一个简单的例子,尽管实际上您可能希望创建一些重载以获得更大的灵活性。(实际上,您只需使用框架的内置
    Split
    方法!)

    string foo=“smith;rodgers;McCalne”;
    foreach(foo.Split2(“;”)中的字符串条)
    {
    控制台写入线(bar);
    }
    // ...
    公共静态类扩展
    {
    公共静态IEnumerable Split2(此字符串源,字符串delim)
    {
    //为简洁起见,省略了参数null检查等
    int oldIndex=0,newIndex;
    而((newIndex=source.IndexOf(delim,oldIndex))!=-1)
    {
    收益率返回源.Substring(oldIndex,newIndex-oldIndex);
    旧索引=新索引+删除长度;
    }
    收益率返回源。子字符串(oldIndex);
    }
    }
    
    您可以创建自己的循环来进行拆分。下面是一个使用
    Aggregate
    扩展方法的方法。效率不是很高,因为它在字符串上使用了
    +=
    运算符,因此除了作为示例外,不应将其真正用作任何东西,但它可以工作:

    string names = "smith;rodgers;McCalne";
    
    List<string> split = names.Aggregate(new string[] { string.Empty }.ToList(), (s, c) => {
      if (c == ';') s.Add(string.Empty); else s[s.Count - 1] += c;
      return s;
    });
    
    string name=“smith;rodgers;McCalne”;
    List split=names.Aggregate(新字符串[]{string.Empty}.ToList(),(s,c)=>{
    if(c==';')s.Add(string.Empty);else s[s.Count-1]+=c;
    返回s;
    });
    
    我不明白这个问题;答案不是“编写拆分方法”吗?写这个方法一点也不难。哈哈,我认为OP本质上是在问如何实现拆分方法。
    string names = "smith;rodgers;McCalne";
    
    List<string> split = names.Aggregate(new string[] { string.Empty }.ToList(), (s, c) => {
      if (c == ';') s.Add(string.Empty); else s[s.Count - 1] += c;
      return s;
    });