duanbarong4321
2017-06-23 04:27 阅读 37

rtrim php相当于C#

I would like to know how to accomplish the PHP

query = rtrim($query, '& ');

in C# ASP .NET MVC

I have tried Strings.RTrim("string") but it does not work.

Have also tried query = query.Trim(query, "&"); but it gives me error.

Also msdn provides code for rtrim in visual basic, which does not help.

From looking at the code it seems like its' trimming the whitespace if found after & character.

Anyone know how to replicate the php command? Cheers

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dou11655853 dou11655853 2017-06-23 04:36

    The .Trim() method removes whitespaces from both the start and end of the string, so

    "   Hello World   ".Trim() -> "Hello World"
    

    For performing the RTrim() you can use the TrimEnd() method with the String.

    "   Hello World   ".TrimEnd() -> "   Hello World"
    
    点赞 评论 复制链接分享
  • doufu9836 doufu9836 2017-06-23 04:37

    try this ==>

    string.Remove(string.LastIndexOf("&"));
    

    With the help of remove last index you can remove the last element

    点赞 评论 复制链接分享
  • douxuanling6523 douxuanling6523 2017-06-23 04:46

    C# Has pretty neat String trimming methods.

    I'm listing different options below. Although, the one you are looking for is

    StringVariable.TrimEnd();
    "This string ends here.  ".TrimEnd();   //This also works.
    

    String-trimming methods

    string message = "    Hello!   ";
    message.TrimEnd();    // Trims from the right side.
    message.TrimStart();  // Trims from the left side.
    message.Trim();       // Trims on both sides
    

    Summary

    message.TrimEnd() will trim from the right side of the string variable.

    message.TrimStart() will trim from the left side of the string variable.

    message.Trim() will trim from both sides of the string variable.

    Also msdn provides code for rtrim in visual basic, which does not help.

    I found a few that might help you, and they have them in multiple .NET languages, including C#.

    Microsoft - String.Trim Method

    Microsoft - String.TrimEnd Method

    Microsoft - String.TrimStart Method

    Hope this helps you :)

    点赞 评论 复制链接分享

相关推荐