By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

The replace() method question???

P: n/a
Hi all,

I searching a string to see if "Desc" resides in it, if so then replace it
with "Description". For some reason if it finds "Desc" in the word
"Description" it replaces the "Desc" part of it with "Description thus having
the final outcome of "Descriptionription". Is there a way to have the
replace() method only replace if the entire string is "Desc" only and not
"Desc* " ? Hope this makes sense. Thanks in advance.

if( "Text" == dcSource.ColumnName.ToString() )
{
string changeContent = drSource[dcSource.ColumnName].ToString();
changeContent = changeContent.Replace("Desc", "Description");
drSource[dcSource.ColumnName] = changeContent;
}
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a

Jon S via DotNetMonster.com wrote:
Hi all,

I searching a string to see if "Desc" resides in it, if so then replace it
with "Description". For some reason if it finds "Desc" in the word
"Description" it replaces the "Desc" part of it with "Description thus having
the final outcome of "Descriptionription".
The reason being, you asked to replace 'Desc' with 'Description', so it
did...
Is there a way to have the
replace() method only replace if the entire string is "Desc" only and not
"Desc* " ?
if(changeContent=="Desc") changeContent="Description";

do for you?
Hope this makes sense. Thanks in advance.

if( "Text" == dcSource.ColumnName.ToString() )
{
string changeContent = drSource[dcSource.ColumnName].ToString();
changeContent = changeContent.Replace("Desc", "Description");
drSource[dcSource.ColumnName] = changeContent;
}


--
Larry Lard
Replies to group please

Nov 17 '05 #2

P: n/a
Hi Larry

Thanks for replying.

The 'changeContent' variable won't solely contain "Desc" it may sometimes be
"Short Desc" or "Genus , Desc , Species" .

So the below line of code doesnt achieve what I would like.
if( replaceContent == "Desc" ) replaceContent = "Description";

I would like to have the replace() method only replace "Desc" if "Desc" is on
its own (as in a word) regardless if there are other words in the
changeContent variable.

For example :
The replace() method SHOULD replace "Desc" with "Description" in the
following :
"Short Desc" or "Genus , Desc , Species" or "Desc Description"

but NOT in the following :
"Description" or "Description , Time".

Thank you.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #3

P: n/a
lstrRetVal = lstrRetVal.Replace("Desc ", "Description");

notice space after Desc

Jon S wrote:
Hi Larry

Thanks for replying.

The 'changeContent' variable won't solely contain "Desc" it may sometimes be
"Short Desc" or "Genus , Desc , Species" .

So the below line of code doesnt achieve what I would like.
if( replaceContent == "Desc" ) replaceContent = "Description";

I would like to have the replace() method only replace "Desc" if "Desc" is on
its own (as in a word) regardless if there are other words in the
changeContent variable.

For example :
The replace() method SHOULD replace "Desc" with "Description" in the
following :
"Short Desc" or "Genus , Desc , Species" or "Desc Description"

but NOT in the following :
"Description" or "Description , Time".

Thank you.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #4

P: n/a
Hi Glen

The space after "desc " didnt work. Any other ideas?

Thanks.
--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #5

P: n/a
lstrRetVal = lstrRetVal.Replace("Desc", "Description").Replace
("Descriptionription", "Description");

Not very elegant but should work.

Jon S wrote:
Hi Glen

The space after "desc " didnt work. Any other ideas?

Thanks.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #6

P: n/a
Thanks Glen, it works.
--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #7

P: n/a
What you want is the text "Desc" surrounded by word boundries, which in
Regular Expression language is "\bDesc\b". In code that's:

string Text = "'Short Desc' or 'Genus, Desc, Species' , 'Description'";
Regex regex = new Regex("\\bDesc\\b");
Console.WriteLine(regex.Replace(Text, "Description"));
prints:
'Short Description' or 'Genus, Description, Species' , 'Description'

Put into you original code:
Regex regexDesc = new Regex("\\bDesc\\b");
// :
// :

if( "Text" == dcSource.ColumnName.ToString() )
{
string changeContent = drSource[dcSource.ColumnName].ToString();
changeContent = regexDesc.Replace(changeContent, "Description");
drSource[dcSource.ColumnName] = changeContent;
}

--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Jon S via DotNetMonster.com wrote:
Hi all,

I searching a string to see if "Desc" resides in it, if so then
replace it with "Description". For some reason if it finds "Desc" in
the word "Description" it replaces the "Desc" part of it with
"Description thus having the final outcome of "Descriptionription".
Is there a way to have the replace() method only replace if the
entire string is "Desc" only and not "Desc* " ? Hope this makes
sense. Thanks in advance.

if( "Text" == dcSource.ColumnName.ToString() )
{
string changeContent = drSource[dcSource.ColumnName].ToString();
changeContent = changeContent.Replace("Desc", "Description");
drSource[dcSource.ColumnName] = changeContent;
}


Nov 17 '05 #8

P: n/a
James Curran <Ja*********@mvps.org> wrote:
What you want is the text "Desc" surrounded by word boundries, which in
Regular Expression language is "\bDesc\b".


And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

P: n/a
>And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription", "Description")

I'd call this 'done easily using simple string operations'.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #10

P: n/a

"Glen W via DotNetMonster.com" <u6961@uwe> wrote in message news:56c2de5df5b5c@uwe...
And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription", "Description")

I'd call this 'done easily using simple string operations'.


It is about as attractive as a pig in high-heels and works about as good.
A Regex is PERFECTLY suited to this sort of task.
Your solution is not only confusing, it is WRONG!!

ANY word that starts with Desc will now start with Description.
Try these: (upper and lower case)
Descarte, Descend, Descendant, descendancy, descent, descramble, describe, descriptive, descry, and
a whole bunch mo more obscure words.
You will need special code for all of them for upper and lower case.

THIS is a job for Regex.
Use the right tool for the job
Bill
Nov 17 '05 #11

P: n/a
Incorrect, Glen. While your method will certainly work, it will require a
great deal more processing and memory to do its work. It will have to parse
the string once to replace the "Desc" in the string, thus generating a new
string, and then re-parse the new string to replace the "DescDescription"
instances, thus generating a third string.

As Jon pointed out, this is what Regular Expressions were designed for.
Using Regex.Replace(), the string is parsed one time, using the powerful
Regular Expression algorithm, and only one string is generated as a
replacement.

So, while both methods are "done easily" on the part of the developer, only
one is "done easily" on the part of the application.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Glen W via DotNetMonster.com" <u6961@uwe> wrote in message
news:56c2de5df5b5c@uwe...
And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription",
"Description")

I'd call this 'done easily using simple string operations'.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1

Nov 17 '05 #12

P: n/a
Seconded (in favor of Regex); as another issue, it would takes away the
user's ability to use the word "Descriptionription" - every time they try,
it will replace it with "Description"... which is not what they said they
wanted!

OK, to be fair, it is unlikely that they are likely to want to use the word
"Descriptionription", but it you used this as a template / design-pattern,
you could very quickly run into some pretty serious problems...

Marc

"Bill Butler" <qw****@asdf.com> wrote in message
news:tz4af.2708$9E4.1962@trndny02...

"Glen W via DotNetMonster.com" <u6961@uwe> wrote in message
news:56c2de5df5b5c@uwe...
>And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription",
"Description")

I'd call this 'done easily using simple string operations'.


It is about as attractive as a pig in high-heels and works about as good.
A Regex is PERFECTLY suited to this sort of task.
Your solution is not only confusing, it is WRONG!!

ANY word that starts with Desc will now start with Description.
Try these: (upper and lower case)
Descarte, Descend, Descendant, descendancy, descent, descramble, describe,
descriptive, descry, and a whole bunch mo more obscure words.
You will need special code for all of them for upper and lower case.

THIS is a job for Regex.
Use the right tool for the job
Bill

Nov 17 '05 #13

P: n/a
>ANY word that starts with Desc will now start with Description.
Try these: (upper and lower case)
Descarte, Descend, Descendant, descendancy, descent, descramble, describe, descriptive, descry, and
a whole bunch mo more obscure words.
You will need special code for all of them for upper and lower case.


Good point, I didn't think of that.
You are correct this is a case when the 'unix sys admin perl-like code', ie.
regex, is the best :)
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #14

P: n/a
What was wrong with Glen.s original suggestion of .Replace("desc ",
"description "), this is both very simple and would seem to fit all the
criteria he was asking for

"Kevin Spencer" wrote:
Incorrect, Glen. While your method will certainly work, it will require a
great deal more processing and memory to do its work. It will have to parse
the string once to replace the "Desc" in the string, thus generating a new
string, and then re-parse the new string to replace the "DescDescription"
instances, thus generating a third string.

As Jon pointed out, this is what Regular Expressions were designed for.
Using Regex.Replace(), the string is parsed one time, using the powerful
Regular Expression algorithm, and only one string is generated as a
replacement.

So, while both methods are "done easily" on the part of the developer, only
one is "done easily" on the part of the application.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Glen W via DotNetMonster.com" <u6961@uwe> wrote in message
news:56c2de5df5b5c@uwe...
And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription",
"Description")

I'd call this 'done easily using simple string operations'.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1


Nov 17 '05 #15

P: n/a
"White Knight" <Wh*********@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
What was wrong with Glen.s original suggestion of .Replace("desc ",
"description "), this is both very simple and would seem to fit all the
criteria he was asking for


except it would fail on : 'Short Desc' or 'Genus, Desc, Species'
since in neither case is "Desc" followed by a space.
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #16

P: n/a
Ahhh, true indeed; I hadn't looked hard enough!!!

"James Curran" wrote:
"White Knight" <Wh*********@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
What was wrong with Glen.s original suggestion of .Replace("desc ",
"description "), this is both very simple and would seem to fit all the
criteria he was asking for


except it would fail on : 'Short Desc' or 'Genus, Desc, Species'
since in neither case is "Desc" followed by a space.
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 17 '05 #17

P: n/a
Glen W via DotNetMonster.com <u6961@uwe> wrote:
And just before anyone reckons I'll be protesting - this is a perfect
place to use regular expressions, as it's doing something which *can't*
easily be done using simple string operations...


Replace("Desc", "Description").Replace("Descriptionription", "Description")

I'd call this 'done easily using simple string operations'.


Not only is it not obvious what's going on, but if for some reason a
field actually contains "Descriptionription", you've lost data. Regular
expressions are the right tool for the job in this case, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #18

P: n/a
James C, I've now used the regex way - thank you.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.