Question regarding C#
-
Any reason for the RegEx rather than just string.Split?
Regards, Rob Philpott.
Because the syntax you have to use to split on string(s) rather than character(s) is so damn clumsy: :laugh:
string\[\] parts = input.Split(new string\[\] { " Is " }, StringSplitOptions.None);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
How to split a String on the basis of a specific word... which means a string: My Name Is Ali i want to split the string when ever Is comes.
try this string s="test1 ali test2 ali"; string[] parts = s.Replace("ali", "/").Split('/');
-
try this string s="test1 ali test2 ali"; string[] parts = s.Replace("ali", "/").Split('/');
And what happens when s contains "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here"?
-
And what happens when s contains "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here"?
static void Main(string[] args) { String s = "aaa ali jskdfhskjdfhk ali sjkhfkjsfhkjsdh ali"; var regex = new Regex("ali", RegexOptions.IgnoreCase); var s1 = regex.Replace(s, "/"); string[] parts = s1.Split('/'); for (int i = 0; i < parts.Length; i++) Console.WriteLine(parts[i]); Console.ReadLine(); }
-
static void Main(string[] args) { String s = "aaa ali jskdfhskjdfhk ali sjkhfkjsfhkjsdh ali"; var regex = new Regex("ali", RegexOptions.IgnoreCase); var s1 = regex.Replace(s, "/"); string[] parts = s1.Split('/'); for (int i = 0; i < parts.Length; i++) Console.WriteLine(parts[i]); Console.ReadLine(); }
Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.
-
Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.
i tried with your string 'My Name Is Ali' also and it return 'My Name Is'
-
Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.
-
Because the syntax you have to use to split on string(s) rather than character(s) is so damn clumsy: :laugh:
string\[\] parts = input.Split(new string\[\] { " Is " }, StringSplitOptions.None);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Fair point... There is a rather obvious overload missing.
Regards, Rob Philpott.
-
i tried with your string 'My Name Is Ali' also and it return 'My Name Is'
You seem to be confusing me with the original poster. My string was "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here". Try that one.
-
You seem to be confusing me with the original poster. My string was "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here". Try that one.
Now i got you, please try bellow
static void Main(string[] args)
{
String s = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
string pattern = @"\bali\b";
string replace = "/";
var s1 = Regex.Replace(s, pattern, replace, RegexOptions.IgnoreCase);
string[] parts = s1.Split('/');
for (int i = 0; i < parts.Length; i++)
Console.WriteLine(parts[i]+ i.ToString());
Console.ReadLine();}
-
Now i got you, please try bellow
static void Main(string[] args)
{
String s = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
string pattern = @"\bali\b";
string replace = "/";
var s1 = Regex.Replace(s, pattern, replace, RegexOptions.IgnoreCase);
string[] parts = s1.Split('/');
for (int i = 0; i < parts.Length; i++)
Console.WriteLine(parts[i]+ i.ToString());
Console.ReadLine();}
Using the
Regex.Split
method[^] (as OG suggested[^]) is a better solution:string input = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
string pattern = @"\bali\b";string[] parts = Regex.Split(input, pattern, RegexOptions.IgnoreCase);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Using the
Regex.Split
method[^] (as OG suggested[^]) is a better solution:string input = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
string pattern = @"\bali\b";string[] parts = Regex.Split(input, pattern, RegexOptions.IgnoreCase);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
thanks..