Windows Develop Bookmark and Share   
 index > Windows Forms General > how to replace exact word from string
 

how to replace exact word from string

Hi ,

string mystring = "abc abcd abc abcde abc abcdef " ,

so i want to remove "abc" from mystring . if i use

mystring.replace("abc"," ")

mystring will be "d de def " ,

but i want "abcd abcde abcdef"

thanks in advance

saumitra

saumitra.tamrakar  Tuesday, October 30, 2007 6:53 AM

Hi, saumitra,

In that case, you can try this sample.

Code Block

string mystr = "abc abc abcd bcd bcde hijk hijkl pqpq pqpqe ";

Regex findDuplicate = new Regex(@"\b([a-z]+) \1\b");

string result=findDuplicate.Replace(mystr,"");

MessageBox.Show(result);

For more regular expression in .Net, please visit

http://msdn2.microsoft.com/en-us/library/hs600312.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Monday, November 05, 2007 1:33 AM

Hi ,
i have to remove comman words from a table in c# which have arround 2 lacs records . for that i list out all the words and there occurance in every row . after
i remove words which occurance more than 10 times .
but that all is taking too much time .
how can reduce the time ,
thanks in advance
saumitra tamrakar
saumitra.tamrakar  Tuesday, October 30, 2007 6:58 AM

Hi,

I suggest you to explore a bit on Regular Expression (Regex class in .NET). I believe that is the only way to go. I will write you a small example once I got back from work later.

Thomas

Thomas Cheah  Tuesday, October 30, 2007 8:28 AM

thanks Thomas

but my actual problem is

i have to remove common words from a table in c# which have arround 2 lacs records . for that i list out all the words and there occurance in every row . after
i remove words which occurance more than 10 times .
but that all is taking too much time .
how can reduce the time ,


saumitra.tamrakar  Tuesday, October 30, 2007 10:11 AM

Hi, saumitra,

For your first qustion, I think it could be done with this line.

Code Block

"abc "," ")

But I am not quite sure of your actual problem.

Could you please show us some more explanation?

Regards

Yu Guo â€?MSFT  Thursday, November 01, 2007 6:01 AM

Hi , Yu Guo

you are correct here but my problem is that

when i list out commom words ,

the count ofcommonwords arearound 3000 then

and if i am going add " " in every word inend andstart that increase too many operation as well then

i have to add " " at start and endof every row of table

that alltakestoo much time

Regards

saumitra.tamrakar  Thursday, November 01, 2007 9:34 AM

Hi, saumitra,

It is still a little confusing for me.

You mean you have 3000 words, and you want to remove some of the words, don't you?

And which part takes too much time?

I think there is no way to reduce the time for words replacing,

Maybe you can consider how to reduce the source words.

Regards

Yu Guo â€?MSFT  Thursday, November 01, 2007 9:46 AM

HI Yu Guo,

I am having around 2 lac records from

which i have to listout all the words and occurance of every word

and then words which having occurance more then a given no

remove from all records .

saumitra.tamrakar  Thursday, November 01, 2007 1:16 PM

Hi, saumitra

I guess you mean that there are are lot of words which may appear more than once, don't you?

Then you will need to use regular expression to find out the words an remove them.

For example,

Code Block

Regex findWords = new Regex(" [^ ]+");//Find the words like " abc"

Regex[] removeWords;

private void button1_Click(object sender, EventArgs e)

{

string test = " abc abcd bcd bcde hijk hijkl pqpq pqpqe"; //My sample data

MatchCollection all = findWords.Matches(test);

removeWords = new Regex[all.Count];

int index = 0;

string[] values = new string[all.Count];

foreach (Match m in all) //List all the words

{

values[index] = m.Value.Trim();

index++;

}

for (int i = 0; i < removeWords.Length; i++)

{

removeWords[i] = new Regex(" " + values[i]);

if (removeWords[i].Matches(test).Count > 1) //If the words appears more than one time

{

removeWords[i] = new Regex(" " + values[i] + " ");

test = removeWords[i].Replace(test, " "); //Remove the first word.

}

}

MessageBox.Show(test);

}

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, November 02, 2007 2:12 AM
Hi Yu Guo ,

u got my problem correct but your solution is not correct

as when i give string
" abc abcd bcd bcde hijk hijkl pqpq pqpqe "
that returns
"abcd bcde hijkl pqpqe"

while it should be
" abc abcd bcd bcde hijk hijkl pqpq pqpqe "
as no word is repeat.
and when i give
"abc abc abcd bcd bcde hijk hijkl pqpq pqpqe "
it should return "abcd bcd bcde hijk hijkl pqpq pqpqe "
thanks
saumitra.tamrakar  Saturday, November 03, 2007 5:51 AM

Hi, saumitra,

In that case, you can try this sample.

Code Block

string mystr = "abc abc abcd bcd bcde hijk hijkl pqpq pqpqe ";

Regex findDuplicate = new Regex(@"\b([a-z]+) \1\b");

string result=findDuplicate.Replace(mystr,"");

MessageBox.Show(result);

For more regular expression in .Net, please visit

http://msdn2.microsoft.com/en-us/library/hs600312.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Monday, November 05, 2007 1:33 AM

Hi Yu Guo ,

thanks a lot , your solution is perfect this time .

regards

saumitra.tamrakar  Monday, November 05, 2007 6:44 AM

You can use google to search for other answers

Custom Search

More Threads

• multi-column combo
• Multiline Textboxes and new lines
• Unable to close MDI app
• What are .pdb files
• delete xml file and create a new xml file with same name
• scrollbar
• Listbox refresh
• Auto Close form after time - VB, VS-05
• split view application
• Forms spawning new Forms