Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 9-Jan-13 20:17pm
bsb25827
Edited 9-Jan-13 20:26pm
v3
Comments
Milind_T at 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?
Milind_T at 10-Jan-13 4:58am
   
Thanks for updating the question. Please check the updated solution below.Milind
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can do these very quickly in C#.
Just use the string.split function.
 
Something like this would work - strArry = myString.split(@",");
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
 
num[0]="this";
num[1]="is";
num[2]="a";
num[3]="sample";
num[4]="string";
  Permalink  
Comments
Matt T Heffron at 10-Jan-13 13:38pm
   
Why the convolution of the Func... GetCount ?
Just use:
string [] num = str.Split(',');
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

use Split function
class Program
{
    static void Main()
    {
    string s = "Apple,Orange,Banana";
    string[] fruits = s.Split(',');
    foreach (string fruit in fruits)
    {
        Console.WriteLine(fruit);
    }
    }
}
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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);
    }
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

MatchCollection mc = Regex.Matches(strList, c.ToString());
if (mc.Count >= numCount)
result = mc[numCount - 1].Index;
 
sIMILAR TO MILIND'S soLN
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 379
1 Nirav Prabtani 266
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,545
1 Sergey Alexandrovich Kryukov 6,757
2 Maciej Los 3,909
3 Peter Leow 3,693
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100