C# 使用<;将一行文本拆分为两行;br>;在c中#

C# 使用<;将一行文本拆分为两行;br>;在c中#,c#,C#,我有以下代码(继承!),它将一行文本拆分为两行,并带有html换行符 public static string BreakLineIntoTwo(this HtmlHelper helper, string input) { if (string.IsNullOrEmpty(input)) return string.Empty; if (input.Length < 12) return input; int pos = input.Length / 2;

我有以下代码(继承!),它将一行文本拆分为两行,并带有html换行符

public static string BreakLineIntoTwo(this HtmlHelper helper, string input)
{
    if (string.IsNullOrEmpty(input)) return string.Empty;
    if (input.Length < 12) return input;

    int pos = input.Length / 2;

    while ((pos < input.Length) && (input[pos] != ' '))
        pos++;

    if (pos >= input.Length) return input;

    return input.Substring(0, pos) + "<br/>" + input.Substring(pos + 1);
}
publicstaticstringbreaklineintoo(此HtmlHelper帮助程序,字符串输入)
{
if(string.IsNullOrEmpty(input))返回string.Empty;
if(input.Length<12)返回输入;
int pos=输入长度/2;
while((pos=input.Length)返回输入;
返回输入子字符串(0,pos)+“
”+输入子字符串(pos+1); }
规则似乎是,如果文本行少于12个字符,则只返回它。如果找不到文本的中间部分,则移动到下一个空格并插入换行符。我们还可以假设结尾没有双空格,也没有额外的空格,文本不仅仅是一行长的字母
abcdefghijkillmnopqrstuvwxyz


这似乎很好,我的问题是
有没有更优雅的方法来解决这个问题?

可能的改进是

private const MinimumLengthForBreak = 12;
private const LineBreakString = "<br />";

public static string BreakLineIntoTwo(this HtmlHelper helper, string input)
{
    if (string.IsNullOrEmpty(input)) return string.Empty;
    if (input.Length < MinimumLengthForBreak ) return input;

    int pos = input.IndexOf(' ', input.Length / 2);
    if (pos < 0) return input; // No space found, not checked in original code

    return input.Substring(0, pos) + LineBreakString + input.Substring(pos + 1);
}
private const最小长度forbreak=12;
private const LineBreakString=“
”; 公共静态字符串breaklineintoo(此HtmlHelper帮助程序,字符串输入) { if(string.IsNullOrEmpty(input))返回string.Empty; if(input.Length

注意:由于我在工作,无法检查atm,因此未检查语法。

可能的改进是

private const MinimumLengthForBreak = 12;
private const LineBreakString = "<br />";

public static string BreakLineIntoTwo(this HtmlHelper helper, string input)
{
    if (string.IsNullOrEmpty(input)) return string.Empty;
    if (input.Length < MinimumLengthForBreak ) return input;

    int pos = input.IndexOf(' ', input.Length / 2);
    if (pos < 0) return input; // No space found, not checked in original code

    return input.Substring(0, pos) + LineBreakString + input.Substring(pos + 1);
}
private const最小长度forbreak=12;
private const LineBreakString=“
”; 公共静态字符串breaklineintoo(此HtmlHelper帮助程序,字符串输入) { if(string.IsNullOrEmpty(input))返回string.Empty; if(input.Length

注意:未检查语法,因为我正在工作,无法检查atm。

您可以使用
IndexOf
而不是自己循环字符串

public static string BreakLineIntoTwo(this HtmlHelper helper, string input)
{
    if (string.IsNullOrEmpty(input)) return string.Empty;
    if (input.Length < 12) return input;

    int pos = input.Length / 2;

    pos = input.IndexOf(' ', pos);

    return input.Substring(0, pos) + "<br/>" + input.Substring(pos + 1);
}
publicstaticstringbreaklineintoo(此HtmlHelper帮助程序,字符串输入)
{
if(string.IsNullOrEmpty(input))返回string.Empty;
if(input.Length<12)返回输入;
int pos=输入长度/2;
pos=输入索引OF(“”,pos);
返回输入子字符串(0,pos)+“
”+输入子字符串(pos+1); }
您可以使用
IndexOf
而不是自己循环字符串

public static string BreakLineIntoTwo(this HtmlHelper helper, string input)
{
    if (string.IsNullOrEmpty(input)) return string.Empty;
    if (input.Length < 12) return input;

    int pos = input.Length / 2;

    pos = input.IndexOf(' ', pos);

    return input.Substring(0, pos) + "<br/>" + input.Substring(pos + 1);
}
publicstaticstringbreaklineintoo(此HtmlHelper帮助程序,字符串输入)
{
if(string.IsNullOrEmpty(input))返回string.Empty;
if(input.Length<12)返回输入;
int pos=输入长度/2;
pos=输入索引OF(“”,pos);
返回输入子字符串(0,pos)+“
”+输入子字符串(pos+1); }
为什么不使用word wrap的css属性呢

使用word wrap属性,可以通过指定断开单词强制长文本换行

检查这个

致意

Myra

为什么不使用word wrap的css属性呢

使用word wrap属性,可以通过指定断开单词强制长文本换行

检查这个

致意

Myra

使用“IndexOf”和“Insert”的简短版本:

    private string BreakLineIntoTwo(string input)
    {
        if (string.IsNullOrEmpty(input)) return string.Empty;
        if (input.Length < 12) return input;          

       int index = input.IndexOf(" ", input.Length/2);

       return index > 0 ? input.Insert(index, "<br />") : input;
    }
private string breaklineintoo(字符串输入)
{
if(string.IsNullOrEmpty(input))返回string.Empty;
if(input.Length<12)返回输入;
int index=input.IndexOf(“”,input.Length/2);
返回索引>0?输入。插入(索引“
”):输入; }

我这样做是作为一个单独的方法,但您应该能够轻松地将其更改为扩展方法。

使用“IndexOf”和“Insert”的简短版本:

    private string BreakLineIntoTwo(string input)
    {
        if (string.IsNullOrEmpty(input)) return string.Empty;
        if (input.Length < 12) return input;          

       int index = input.IndexOf(" ", input.Length/2);

       return index > 0 ? input.Insert(index, "<br />") : input;
    }
private string breaklineintoo(字符串输入)
{
if(string.IsNullOrEmpty(input))返回string.Empty;
if(input.Length<12)返回输入;
int index=input.IndexOf(“”,input.Length/2);
返回索引>0?输入。插入(索引“
”):输入; }

我将此作为一个单独的方法来执行,但您应该能够轻松地将其更改为扩展方法。

字符串有任何问题。请替换(“\n”,“
”)
?@Mehrdad-我的文本中没有\n要替换的内容。我想你是想在html方面实现这一点。如果你将文本放入一个具有word wrap css属性的容器中,当文本长度小于容器时,该属性会做完全相同的事情,该怎么办?@Myra-我对此持开放态度,想分享一个展示例子的链接吗?我不知道你想在哪里使用这个方法。如果它的唯一功能是将文本与显示区域相匹配,那么最好使用CSS。字符串有任何错误。请替换(“\n”,“
”)
?@Mehrdad-我的文本中没有\n要替换的内容。我想你是想在html方面实现这一点。如果你将文本放入一个具有word wrap css属性的容器中,当文本长度小于容器时,该属性会做完全相同的事情,该怎么办?@Myra-我对此持开放态度,想分享一个展示例子的链接吗?我不知道你想在哪里使用这个方法。如果它的唯一功能是将文本匹配到显示区域,那么最好使用CSS。不确定这是否与IE7和IE8兼容。在这两个版本上都兼容。检查msdn文档不确定这是否与IE7和IE8兼容。在这两个版本上都兼容。检查msdn文档这不检查索引是否找不到角色,所以它与