tags:

views:

76

answers:

3
string temp_constraint = row["Constraint_Name"].ToString();
string split_string = "FK_"+tableName+"_";
string[] words = Regex.Split(temp_constraint, split_string);

I am trying to split a string using another string.

temp_constraint = FK_ss_foo_ss_fee
split_string = FK_ss_foo_

but it returns a single dimension array with the same string as in temp_constraint

Please help

+1  A: 

You should use String.Split instead

string[] words = 
    temp_constraint.Split(new []{split_string}, StringSplitOptions.None);
Yuriy Faktorovich
Even the method you suggested doesnt work. I tried that too. I am not sure why it doesnt work when I am using a variable to construct split_string variable as such. Instead I used string.replace(splitstring, ""); and it worked properly and it serves my purpose.
Yash
+1  A: 

Your split operation works fine for me:

string temp_constraint = "FK_ss_foo_ss_fee";
string split_string = "FK_ss_foo_";
string[] words = Regex.Split(temp_constraint, split_string);
foreach (string word in words)
{
    Console.WriteLine(">{0}<", word);
}

Output:

><
>ss_fee<

I think the problem is that your variables are not set to what you think they are. You will need to debug to find the error elsewhere in your program.

I would also avoid using Split for this (both Regex and String.Split). You aren't really splitting the input - you are removing a string from the start. Split might not always do what you want. Imagine if you have a foreign key like the following:

FK_ss_foo_ss_fee_FK_ss_foo_ss_bee

You want to get ss_fee_FK_ss_foo_ss_bee but split would give you ss_fee_ and ss_bee. This is a contrived example, but it does demonstrate that what you are doing is not a split.

Mark Byers
the problem is occurring at the split_string = "FK_"+tableName+"_";I used a break point to check the value of tableName and it is exactly showing what I wanted. I am not understanding the reason. When I use a static string, the splitting is occuring properly. But its not the same when I am using the variable
Yash
@Yash: Your variable probably contains some symbol that is not what you think it is. Perhaps an extra space. The problem is most likely with the contents of 'tableName'.
Mark Byers
A: 

string split uses a character array to split text and does the split by each character which is not often ideal.

The following article shows how to split text by an entire word

http://www.bytechaser.com/en/functions/ufgr7wkpwf/split-text-by-words-and-not-character-arrays.aspx

Alex