2

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

Web Dev Guy
  • 1,693
  • 3
  • 18
  • 42

3 Answers3

6

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"
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
2

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 :)

Sometowngeek
  • 597
  • 1
  • 6
  • 27
0

try this ==>

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

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

Narayan
  • 1,670
  • 1
  • 19
  • 37