String.Replace

replace any string started with

Page 1 of 1

11 Replies - 1351 Views - Last Post: 15 September 2010 - 05:35 AM Rate Topic: -----

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

String.Replace

Posted 06 September 2010 - 04:05 PM

I want to replace some string

string tempBody=body.Replace("Ahmedn1", "Ahmedn32004@yahoo.com")



this is the code to replace
but suppose that I want to replace any word beginning with "ahm"
is there something like that?

in databases we can use the 'Like' statements to do this

any ideas?
Is This A Good Question/Topic? 0
  • +

Replies To: String.Replace

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: String.Replace

Posted 06 September 2010 - 04:54 PM

You could use Regex.
http://msdn.microsof...ex.replace.aspx
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: String.Replace

Posted 07 September 2010 - 08:16 AM

Quote

replace any word beginning with "ahm"


This is a bit vague. When you way "word" do you mean any string? What length, or what delimiter? SQL's LIKE operator will match strings that start with/end with/contain the provided value. String has methods that simulate these functions (StartsWith, EndsWith, Contains.)

Regex is a good idea if you know how to use it. But one of my favorite old quotes is:

Quote

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Was This Post Helpful? 2
  • +
  • -

#4 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: String.Replace

Posted 07 September 2010 - 08:20 AM

how did I forget that?

(StartsWith, EndsWith, Contains.) can help

but isn't there more direct way other than Regex ?
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: String.Replace

Posted 07 September 2010 - 08:25 AM

You can use .IndexOf() in a loop, incrementing the 'Start' parameter on your loop until you've completed the whole string.

Then you can do an index-based replacement of the words.
Was This Post Helpful? 0
  • +
  • -

#6 coultertech  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 18-May 09

Re: String.Replace

Posted 13 September 2010 - 09:14 PM

string[] splitbody = body.Split(" ");
string rebuildbody = "";
foreach(string s in splitbody)
{
    if (s.StartsWith("ahm"))
    {
        s = "replacement string";
    }
    rebuildbody += s + " ";
}
rebuildbody = rebuildbody.Trim();


Was This Post Helpful? 1
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: String.Replace

Posted 14 September 2010 - 04:32 AM

View Postcoultertech, on 14 September 2010 - 12:14 AM, said:

string[] splitbody = body.Split(" ");
string rebuildbody = "";
foreach(string s in splitbody)
{
    if (s.StartsWith("ahm"))
    {
        s = "replacement string";
    }
    rebuildbody += s + " ";
}
rebuildbody = rebuildbody.Trim();



please use a StringBuilder instead of concatenating strings.

string[] splitbody = body.Split(" ");
StringBuilder rebuildbody = new StringBuilder();
foreach(string s in splitbody)
{
    if (s.StartsWith("ahm"))
    {
        s = "replacement string";
    }
    rebuildbody.Append(s + " ");
}

string finalString = sb.ToString().Trim();


Was This Post Helpful? 4
  • +
  • -

#8 coultertech  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 18-May 09

Re: String.Replace

Posted 14 September 2010 - 10:32 AM

Quote

rebuildbody.Append(s + " ");


Hi, Can you please explain the major difference between what string builder does when it performs an Append and what string + " " + string does? Back before .net there were no string bulders and in fact there were no strings so the only way to do it was to concat strings or characters. seems .net is just giving you a function to do string + string for you. Thanks for any Explanation you can provide. I'm always learning. :-)
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: String.Replace

Posted 14 September 2010 - 11:06 AM

Strings are immutable once created. StringBuilders are not.

Read this MSDN entry on System.String. Scroll down to the "Immutability and the StringBuilder Class" section.

Edit: Copy/Pasted for convenience:

Quote

A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.

Because strings are immutable, string manipulation routines that perform repeated additions or deletions to what appears to be a single string can extract a significant performance penalty. For example, the following code uses a random number generator to create a string with 1000 characters in the range 0x0001 to 0x052F. Although the code appears to use string concatenation to append a new character to the existing string named str, it actually creates a new String object for each concatenation operation.

code snippet removed

You can use the StringBuilder class instead of the String class for operations that make multiple changes to the value of a string. Unlike instances of the String class, StringBuilder objects are mutable; when you concatenate, append, or delete substrings from a string, the operations are performed on a single string. When you have finished modifying the value of a StringBuilder object, you can call its StringBuilder.ToString method to convert it to a string. The following example replaces the String used in the previous example to concatenate 1000 random characters in the range to 0x0001 to 0x052F with a StringBuilder object.

code snippet removed

This post has been edited by insertAlias: 14 September 2010 - 11:10 AM

Was This Post Helpful? 3
  • +
  • -

#10 coultertech  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 18-May 09

Re: String.Replace

Posted 14 September 2010 - 12:35 PM

nice.
Was This Post Helpful? 1
  • +
  • -

#11 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: String.Replace

Posted 15 September 2010 - 05:15 AM

Just remember that a StringBuilder should not always be used for concatenation. With a StringBuilder you have the overhead of creating an instance of a class, and method calls. For these reasons it should only be used if you have >10 concatenations to do, otherwise simple string1 + string2 + string3 is faster.

I actually tested this yesterday and it was always the case.

Of course, the results are extremely noticable once you start getting a large amount of concatenations:

Quote

Running Speed Test on 'Big (5000x) Concatenation Test'

Results
string1+string2+string3+string4+ .. to 5000: 0821310ms
StringBuilder.Append() called 5000x: 0005013ms

Test Difference: Second test outperformed by 0816297ms
Press r to Re-Run, q to Quit, or m for Menu:

This post has been edited by RudiVisser: 15 September 2010 - 05:16 AM

Was This Post Helpful? 4
  • +
  • -

#12 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: String.Replace

Posted 15 September 2010 - 05:35 AM

View PostRudiVisser, on 15 September 2010 - 04:15 AM, said:

Just remember that a StringBuilder should not always be used for concatenation. With a StringBuilder you have the overhead of creating an instance of a class, and method calls. For these reasons it should only be used if you have >10 concatenations to do, otherwise simple string1 + string2 + string3 is faster.

I actually tested this yesterday and it was always the case.

Of course, the results are extremely noticable once you start getting a large amount of concatenations:

Quote

Running Speed Test on 'Big (5000x) Concatenation Test'

Results
string1+string2+string3+string4+ .. to 5000: 0821310ms
StringBuilder.Append() called 5000x: 0005013ms

Test Difference: Second test outperformed by 0816297ms
Press r to Re-Run, q to Quit, or m for Menu:


Interesting to see that, I love numbers from benchmarks. Thanks! +1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1