2

I've got this code,which is going to reverse a text input. It doesn't capture newlines, so I want to check each time whether we encounter a newline in order to insert a newline in my result string manually.

How is it possible?

var a = textBox1.Text;
var c = Environment.NewLine;
string b = "";
foreach(var ch in a)
{
   if (ch.ToString() ==c)
      b += c;
   else
      b = ch + b;
   b += "\n";
}
textBox2.Text = b;
Clipboard.SetText(b);
Brian
  • 5,069
  • 7
  • 37
  • 47
Geekmard
  • 166
  • 1
  • 3
  • 10

2 Answers2

4

You can use Split to get all lines, then reverse each line.

String a = textBox1.Text;
String result = String.Empty;

String[] lines = a.Split(new String[] { Environment.NewLine }, StringSplitOptions.None);

foreach(String line in lines.Reverse())
{
    // inverse text
    foreach(char ch in line.Reverse())
    {
        result += ch;
    }

    // insert a new line
    result += Environment.NewLine;
}

// remove last NewLine
result = result.Substring(0, result.Length - 1);

Exemple : In entry, if you have :

test
yopla

result will be :

alpoy
tset
Xaruth
  • 4,034
  • 3
  • 19
  • 26
2

Your question has the following components:

  • How to break a string down into lines (handling the fact that line breaks might be represented as \n or \r\n).
  • How to reverse a given line, properly dealing with unicode complexities including surrogate pairs and combining characters.

The following extension methods handle both tasks:

public static class TextExtensions
{
    public static IEnumerable<string> TextElements(this string s)
    {
        // StringInfo.GetTextElementEnumerator is a .Net 1.1 class that doesn't implement IEnumerable<string>, so convert
        if (s == null)
            yield break;
        var enumerator = StringInfo.GetTextElementEnumerator(s);
        while (enumerator.MoveNext())
            yield return enumerator.GetTextElement();
    }

    public static string Reverse(this string s)
    {
        if (s == null)
            return null;
        return s.TextElements().Reverse().Aggregate(new StringBuilder(s.Length), (sb, c) => sb.Append(c)).ToString();
    }

    public static IEnumerable<string> ToLines(this string text)
    {
        // Adapted from http://stackoverflow.com/questions/1508203/best-way-to-split-string-into-lines/6873727#6873727
        if (text == null)
            yield break;
        using (var sr = new StringReader(text))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }

    public static string ToText(this IEnumerable<string> lines)
    {
        if (lines == null)
            return null;
        return lines.Aggregate(new StringBuilder(), (sb, l) => sb.AppendLine(l)).ToString();
    }

    public static string ReverseLines(this string s)
    {
        if (s == null)
            return null;
        return s.ToLines().Reverse().Select(l => l.Reverse()).ToText();
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340