#nullable enable
using System.Text;
namespace MyHelpers
{
public static class LineEndingsConverter
{
/// <summary>
/// Converts all "\r" and "\n" line breaks in a string to "\r\n", leaving existing "\r\n" line breaks unchanged.
/// </summary>
/// <param name="input">The input string to convert line breaks for.</param>
/// <returns>The input string with all "\r" and "\n" line breaks converted to "\r\n".</returns>
public static string? ConvertLineEndings(in string? input)
{
if (input is null or "")
{
return input;
}
var sb = new StringBuilder(input.Length);
bool previousWasCarriageReturn = false;
foreach (char c in input)
{
if (c == '\r')
{
if (previousWasCarriageReturn)
{
sb.Append(c);
previousWasCarriageReturn = false;
}
else
{
previousWasCarriageReturn = true;
}
}
else if (c == '\n')
{
if (previousWasCarriageReturn)
{
sb.Append("\r\n");
previousWasCarriageReturn = false;
}
else
{
sb.Append("\r\n");
}
}
else
{
if (previousWasCarriageReturn)
{
sb.Append("\r\n");
previousWasCarriageReturn = false;
}
sb.Append(c);
}
}
if (previousWasCarriageReturn)
{
sb.Append("\r\n");
}
return sb.ToString();
}
}
}
测试:
#nullable enable
using MyHelpers;
using FluentAssertions;
using Xunit;
namespace MyHelpers.Tests
{
public class LineEndingsConverterFixture
{
[Fact]
public void Given_NullInput_When_ConvertingLineEndings_Then_ReturnsNull()
{
// Given
string? input = null;
// When
var output = LineEndingsConverter.ConvertLineEndings(input);
// Then
output.Should().BeNull();
}
[Fact]
public void Given_EmptyInput_When_ConvertingLineEndings_Then_ReturnsEmptyString()
{
// Given
string? input = "";
// When
var output = LineEndingsConverter.ConvertLineEndings(input);
// Then
output.Should().Be("");
}
[Fact]
public void Given_InputWithOnlyCarriageReturn_When_ConvertingLineEndings_Then_ReturnsInputWithCarriageReturnAndLineFeed()
{
// Given
string? input = "Hello\rWorld\r";
// When
var output = LineEndingsConverter.ConvertLineEndings(input);
// Then
output.Should().Be("Hello\r\nWorld\r\n");
}
[Fact]
public void Given_InputWithOnlyNewline_When_ConvertingLineEndings_Then_ReturnsInputWithCarriageReturnAndLineFeed()
{
// Given
string? input = "Hello\nWorld\n";
// When
var output = LineEndingsConverter.ConvertLineEndings(input);
// Then
output.Should().Be("Hello\r\nWorld\r\n");
}
[Fact]
public void Given_InputWithMixedLineBreaks_When_ConvertingLineEndings_Then_ReturnsInputWithCarriageReturnAndLineFeed()
{
// Given
string? input = "Hello\r\nWorld\n";
// When
var output = LineEndingsConverter.ConvertLineEndings(input);
// Then
output.Should().Be("Hello\r\nWorld\r\n");
}
}
}
3条答案
按热度按时间sulc1iza1#
我发现UWPBox可以帮助你。
超声波 Package 盒:https://github.com/ruffin--/UWPBox
正如作者所说:
让我强调一下第一个。即使Environment.NewLine是\r\n,并且您在myTextBox.Text上都有\r\n,如果您选中myTextBox.SelectedText,至少在Windows 10 PC版中,所有换行符都将转换为\r!!!这使得对SelectedText的更改非常古怪地集成回Text的上下文。请参阅StackOverflow问题以获取具体示例。
xdyibdwo2#
如果您尝试在UWP(CS)Windows 10应用中读取文本框中的行,请仅使用**"\r”**,
您可以使用联接插入**"\r\n”**
vc6uscn93#
如果您希望始终使用
\r\n
作为换行符,无论TextBox.Text
为您提供\r
还是\n
(就像Uno平台中的Android/iOS),请执行以下操作。在XAML文件中,将转换器命名空间添加到顶级元素:
将转换器作为静态资源添加到XAML文件中:
将
TextBox.Text
属性绑定到ViewModel中的属性,并使用LineEndingsConverter
转换行尾:一个二个一个一个
实施
测试: