C# 如何检查字符串是否有换行符,然后执行某些操作
我有这个代码,它将反转文本输入。它不捕获换行符,所以我想每次检查是否遇到换行符,以便手动在结果字符串中插入换行符 怎么可能呢C# 如何检查字符串是否有换行符,然后执行某些操作,c#,windows,string,C#,Windows,String,我有这个代码,它将反转文本输入。它不捕获换行符,所以我想每次检查是否遇到换行符,以便手动在结果字符串中插入换行符 怎么可能呢 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 =
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);
您可以使用
Split
获取所有行,然后反转每行
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);
示例:在条目中,如果您有:
test
yopla
结果将是:
alpoy
tset
您的问题包含以下内容:
- 如何将字符串分解为行(处理断行可能表示为
或\n
的事实)\r\n
- 如何反转给定行,正确处理unicode的复杂性,包括
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();
}
}
公共静态类文本扩展
{
公共静态IEnumerable TextElements(此字符串为s)
{
//StringInfo.GetTextElementEnumerator是一个未实现IEnumerable的.Net 1.1类,因此转换
如果(s==null)
屈服断裂;
变量枚举器=StringInfo.GetTextElementEnumerator;
while(枚举数.MoveNext())
产生返回枚举数。GetTextElement();
}
公共静态字符串反转(此字符串为s)
{
如果(s==null)
返回null;
返回s.TextElements().Reverse().Aggregate(新的StringBuilder(s.Length),(sb,c)=>sb.Append(c)).ToString();
}
公共静态IEnumerable ToLines(此字符串文本)
{
//改编自http://stackoverflow.com/questions/1508203/best-way-to-split-string-into-lines/6873727#6873727
if(text==null)
屈服断裂;
使用(var sr=新的StringReader(文本))
{
弦线;
而((line=sr.ReadLine())!=null)
{
收益率回归线;
}
}
}
公共静态字符串ToText(此IEnumerable行)
{
如果(行==null)
返回null;
返回line.Aggregate(新的StringBuilder(),(sb,l)=>sb.AppendLine(l)).ToString();
}
公共静态字符串反转线(此字符串为s)
{
如果(s==null)
返回null;
返回s.ToLines().Reverse().Select(l=>l.Reverse()).ToText();
}
}
文本框是多行的吗?我认为单行文本框不会接受回车。请看这里:该代码不会反转文本。用Aéo
试试。可能是重复的