title: Using a for loop to replace string characters in a .NET C# Model
body: <p>I am new to .NET MVC, and come from PHP/Java/ActionScript. </p>
  
  <p>The problem I have come across is with the .NET Model and <code>get{}</code>. I do not understand why my <code>Hyphenize</code> string will return the value of <code>SomeText</code> truncated to 64 characters, but without replacing any of the characters defined in the array.</p>
  
  <p>Model -  This is supposed to replace certain characters in <code>SomeText</code> with a simple hyphen <code>-</code> : </p>
  
  <pre><code>    public string SomeText{ get; set;} // Unmodified string
  
      public string Hyphenize{ 
          get {
              //unwanted characters to replace
              string[] replace_items = {"#", " ", "!", "?", "@", "*", ",", ".", "/", "'", @"\", "=" };
              string stringbuild = SomeText.Substring(0, (SomeText.Length &gt; 64 ? 64 : SomeText.Length));
  
              for (int i = 0; i &lt; replace_items.Length; i++)
              {
                  stringbuild.Replace(replace_items[i], "-");
              }
  
              return stringbuild;
          }
  
          set { }
      }
  </code></pre>
  
  <p>Alternatively, the method below does work correctly and will return the string with <code>" "</code> and <code>"#"</code> characters replaced. However, it bothers me that I am unable to understand why the for loop did not work.</p>
  
  <pre><code>    public string Hyphenize{ 
          get {
              //Replaces unwanted characters
              return SomeText.Substring(0, (SomeText.Length &gt; 64 ? 64 : SomeText.Length)).Replace(" ", "-").Replace("#", "-");
          }
  
          set { }
      }
  </code></pre>
  
  <p>Ultimately I ended up with </p>
  
  <pre><code>return Regex.Replace(SomeText.Substring(0, (SomeText.Length &gt; 64 ? 64 : SomeText.Length)).Replace("'", ""), @"[^a-zA-Z0-9]", "-").Replace("--", "-");
  </code></pre>