Deleting words from string
-
Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R
-
Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R
You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".
-
You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".
what you could do is keep it simple :) use string.Replace. ex: string x = "This is a test,test"; string y = x.Replace("test",""); Console.WriteLine(y); output should look like "This is a , " have fun Kaine
-
what you could do is keep it simple :) use string.Replace. ex: string x = "This is a test,test"; string y = x.Replace("test",""); Console.WriteLine(y); output should look like "This is a , " have fun Kaine
Damn, string.Replace. How could i forget that. Oh well, time to go methinks. On a side note, i appear to have found a new forum to live on for a while. Who would have thought it'd be here?
My current favourite word is: Waffle Cheese is still good though.
-
You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".
Thanks to both of you that replied!! R
-
Thanks to both of you that replied!! R
But what if you were to remove a word like '{test}' including the braces?
-
But what if you were to remove a word like '{test}' including the braces?
Nevermind I got it now, I'm just being stupid today... Thanks again guys!
-
You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".
Is there a neater more professional way to re-write the code below?
public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }
-
Is there a neater more professional way to re-write the code below?
public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }
Do you mean something like this? Regex.Replace(statementString, @"\[|dbo].|\]", ""); instead of the function Greetings Kaine
-
Do you mean something like this? Regex.Replace(statementString, @"\[|dbo].|\]", ""); instead of the function Greetings Kaine
I was thinking of Regex but wasn't sure. Thanks for that Kain!
-
Is there a neater more professional way to re-write the code below?
public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }
I'm not sure why you're removing the brackets and 'dbo' from SQL. It could open you up to syntax errors, especially since you don't account for the context whatsoever in these sort of find/replace methods. Plus, I remember reading somewhere that stating 'dbo' will improve lookup performance ;) But whatever...just throwing that out there as a word of caution.
-
Is there a neater more professional way to re-write the code below?
public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }
This one is more concise and more efficient since it doesn't create unnecessary temporary string instances.
public string StripString(string statementString)
{
string[] keywords = { "[dbo].", "[", "]" };
StringBuilder result = new StringBuilder(statementString);
foreach (string keyword in keywords) result.Replace(keyword, string.Empty);return result.ToString();
}Now, let me ask you a question. It appears you're trying to parse a connection string - is that what you're trying to do? Are you aware there's already a class in the .NET framework that parses a connection string and give you back the interesting pieces of information? That would be OdbcConnection; pass a connection string in the constructor and you'll have a connection object containing all the properties of the connection string.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Feast of Tabernacles (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R
string s = "this is a test"; s.Replace("test", "");
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R
Hi, all replies may lead to damaged sentences such as "Nuclear s damaged the conants inines", and basically ignore the subject line. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Hi, all replies may lead to damaged sentences such as "Nuclear s damaged the conants inines", and basically ignore the subject line. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
Actually, i think a couple of the replies inlcuded a space after the word test, so "test ". That would only ruin words ending in test. And besides, if you do it the way i said originally (though of course, i had forgotten about string.replace) you can remove " test " with spaces either side. And then when you re-build the string, add the spaces back in.
My current favourite word is: Waffle Cheese is still good though.
-
Actually, i think a couple of the replies inlcuded a space after the word test, so "test ". That would only ruin words ending in test. And besides, if you do it the way i said originally (though of course, i had forgotten about string.replace) you can remove " test " with spaces either side. And then when you re-build the string, add the spaces back in.
My current favourite word is: Waffle Cheese is still good though.
Words are not necessarily surrounded by spaces, there could be punctuation marks too. This contest holds two "test" words for you to test. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Words are not necessarily surrounded by spaces, there could be punctuation marks too. This contest holds two "test" words for you to test. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
Yar, stupid punctuation, ruined the english language. :rolleyes: I concede to your brilliance.
My current favourite word is: Waffle Cheese is still good though.