服务器端修复文本字符串中的寡妇

时间:2017-07-17 20:22:02

标签: javascript c# .net razor

我遇到服务器端寡妇修复问题。 我能够通过客户端javascript修复寡妇/孤儿。 但是我更喜欢在页面渲染之前做服务器端。不幸的是我的C#是有限的。我怎么能做到这一点?或者至少知道如何实现这一目标 这是我使用过的javascript。

 var wordArray = $('element').text().split(' ');
 if (wordArray.length > 1) {
    wordArray[wordArray.length - 2] += ' ' + wordArray[wordArray.length 
   - 1];
   wordArray.pop();
   $(''element'').html(wordArray.join(' '));
  }

提前谢谢。

1 个答案:

答案 0 :(得分:0)

请阅读我在代码中的评论

public string Model_fix(string ElementText)
{
    var result = string.Empty;
    // the compiler will set wordArray as List of string and not an array
    // list in c# is more similar to an array in javascript
    var wordArray = ElementText.ToString().Split(' ').ToList(); 
    if (wordArray.Count > 1)
    {
        wordArray[wordArray.Count - 2] += "' '" + wordArray[wordArray.Count - 1];
        wordArray.RemoveAt(wordArray.Count - 1); // equivilent to Array.pop in javascript
        result = string.Join(" ", wordArray); // equivilent to Array.join() in javascript
    }

    return result;
}