Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted
Updated 9-Jan-13 20:26pm
v3
Comments
MT_ 10-Jan-13 2:21am    
WHat have you done so far? In the question you are saying in "SQL" and tag says "C#", which one is true?
MT_ 10-Jan-13 4:58am    
Thanks for updating the question. Please check the updated solution below.Milind

Take a look at String.Split

C#
string foo = "a,b,c";

string [] foos = foo.Split(new char [] {','});

foreach(var item in foos)
{
   Console.WriteLine(item);
}
 
Share this answer
 
You can do these very quickly in C#.
Just use the string.split function.

Something like this would work - strArry = myString.split(@",");
 
Share this answer
 
In C#, simply use .Split

C#
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

C#
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
 
Share this answer
 
v3
C#
string str = "this,is,a,sample,string";

            Func<string, string []> GetCount = x => x.Split(',');

            string [] num = GetCount(str);


would return you string array of length 5 like

C#
num[0]="this";
num[1]="is";
num[2]="a";
num[3]="sample";
num[4]="string";
 
Share this answer
 
Comments
Matt T Heffron 10-Jan-13 13:38pm    
Why the convolution of the Func... GetCount ?
Just use:
string [] num = str.Split(',');
use Split function
C#
class Program
{
    static void Main()
    {
    string s = "Apple,Orange,Banana";
    string[] fruits = s.Split(',');
    foreach (string fruit in fruits)
    {
        Console.WriteLine(fruit);
    }
    }
}
 
Share this answer
 
C#
class Program
{
    static void Main()
    {
    string s = "there is a cat";
    //
    // Split string on spaces.
    // ... This will separate all the words.
    //
    string[] words = s.Split(' ');
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }
    }
}
 
Share this answer
 
MatchCollection mc = Regex.Matches(strList, c.ToString());
if (mc.Count >= numCount)
result = mc[numCount - 1].Index;

sIMILAR TO MILIND'S soLN
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900