Replaces the new lines in a string with the given replacement characters. : String Replace « Data Types « C# / C Sharp






Replaces the new lines in a string with the given replacement characters.

    

#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace Newtonsoft.Utilities.Text
{
  public static class StringExtensions
  {

    /// <summary>
    /// Replaces the new lines in a string with the given replacement characters.
    /// </summary>
    /// <param name="s">The string to replace new lines in.</param>
    /// <param name="replacement">The replacement characters.</param>
    /// <returns></returns>
    public static string ReplaceNewLines(this string s, string replacement)
    {
      StringReader sr = new StringReader(s);
      StringBuilder sb = new StringBuilder();

      bool first = true;

      string line;
      while ((line = sr.ReadLine()) != null)
      {
        if (first)
          first = false;
        else
          sb.Append(replacement);

        sb.Append(line);
      }

      return sb.ToString();
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Replace char inside a string
2.use the Insert(), Remove(), and Replace() methods to modify strings
3.String reverse and replaceString reverse and replace
4.Inserting, replacing, and removingInserting, replacing, and removing
5.String insert and outputString insert and output
6.remove any of a set of chars from a given string.
7.Replaces the specified string to replace.
8.Replace Once