The replace() method question???

  • Thread starter Thread starter Jon S via DotNetMonster.com
  • Start date Start date
J

Jon S via DotNetMonster.com

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;
}
 
Jon said:
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;
}
 
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.
 
lstrRetVal = lstrRetVal.Replace("Desc ", "Description");

notice space after Desc
 
Hi Glen

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

Thanks.
 
lstrRetVal = lstrRetVal.Replace("Desc", "Description").Replace
("Descriptionription", "Description");

Not very elegant but should work.
 
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;
}
 
James Curran said:
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...
 
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'.
 
Glen W via DotNetMonster.com said:
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
 
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.
 
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
 
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 :)
 
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 said:
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.
 
White Knight said:
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
 
Ahhh, true indeed; I hadn't looked hard enough!!!

James Curran said:
White Knight said:
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
 
Glen W via DotNetMonster.com said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top