将字符串拆分为字符串数组。

编程入门 行业动态 更新时间:2024-10-12 18:19:34
本文介绍了将字符串拆分为字符串数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在C#中执行sql查询时遇到问题。当字符串在IN CLAUSE中包含超过1000个enteries时,sql查询会抛出错误。该字符串有超过1000个子字符串,每个子字符串由'','分隔。 我想将字符串拆分成字符串数组,每个字符串包含999个字符串,分别用'',''。 或 如何在字符串中找到'',''的第n个出现。

解决方案

看看String.Split

string foo = a,b,c; string [] foos = foo.Split( new char [] {' ,'}); foreach ( var item in foos) { Console.WriteLine(item); }

你可以在C#中快速完成这些。 只需使用字符串.split 功能。 这样的东西可行 - strArry = myString.split(@,) ;

在C#中,只需使用.Split

string STR = abc,def,ghi,jkl, MNO,PQR; string [] phrase = STR.Split(' ); string nthPhrase = phrase [n];

或者,您可以使用RegEx获取数字如下

字符串 s = abc,def,ghi,jkl,mno,pqr; int n = 3 ; int j = -1; MatchCollection myMatches = Regex.Matches(s, ,); 如果(myMatches.Count > = n-1) {j = myMatches [n - 1 ]。索引; }

希望有所帮助,如果它确实标记为answe / upvote。 Milind

I am facing a problem while executing a sql query in C#.The sql query throws an error when the string contains more than 1000 enteries in the IN CLAUSE .The string has more than 1000 substrings each seperated by '',''. I want to split the string into string array each containing 999 strings seperated by '',''. or How can i find the nth occurence of '','' in a string.

解决方案

Take a look at String.Split

string foo = "a,b,c"; string [] foos = foo.Split(new char [] {','}); foreach(var item in foos) { Console.WriteLine(item); }

You can do these very quickly in C#. Just use the string.split function. Something like this would work - strArry = myString.split(@",");

In C#, simply use .Split

string STR = "abc,def,ghi,jkl,mno,pqr"; string[] phrases = STR.Split(','); string nthPhrase = phrases[n];

Alternatively, you can use RegEx to get the number as below

String s = "abc,def,ghi,jkl,mno,pqr"; int n = 3; int j=-1; MatchCollection myMatches = Regex.Matches(s, ","); if (myMatches.Count >= n-1 ) { j = myMatches[n - 1].Index; }

Hope that helps, If it does mark it as answe/upvote. Milind

更多推荐

将字符串拆分为字符串数组。

本文发布于:2023-11-29 22:31:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!