VoidKing
VoidKing

Reputation: 6412

Why doesn't C# .TrimEnd work with the @ symbol?

I have a string that ends with @ and while trying to TrimEnd this string (I only want to trim it if an @ symbol is, indeed, at the end of this string), I find C# is completely ignoring this.

I have written (with Razor; and this is an Ajax request) two hidden input fields to hold the string value both before and after the .TrimEnd('@') call, and the value remains unchanged.

There is no room for this value to be encoded as the string is built in C# and doesn't leave this environment (which goes ultimately to a database) before I can prove that it's not working.

(the variable 'NFPA' contains the string '3-2-0')

string NFPAFPRACHGLOES = "";

if (!String.IsNullOrWhiteSpace(NFPA))
{
    NFPAFPRACHGLOES += NFPA + "\n";
}

if (FBtn == "on")
{
    NFPAFPRACHGLOES += "F";
}

if (PBtn == "on")
{
    if (NFPAFPRACHGLOES.Contains("F"))
    {
        NFPAFPRACHGLOES += "/";
    }

    NFPAFPRACHGLOES += "P";
}

if (RBtn == "on")
{
    if (NFPAFPRACHGLOES.Contains("F") || NFPAFPRACHGLOES.Contains("P"))
    {
        NFPAFPRACHGLOES += "/";
    }

    NFPAFPRACHGLOES += "R";
}

if (ABtn == "on")
{
    if (NFPAFPRACHGLOES.Contains("F") || NFPAFPRACHGLOES.Contains("P") || NFPAFPRACHGLOES.Contains("R"))
    {
        NFPAFPRACHGLOES += "/";
    }

    NFPAFPRACHGLOES += "A";
}

if (CHBtn == "on")
{
    if (NFPAFPRACHGLOES.Contains("F") || NFPAFPRACHGLOES.Contains("P") || NFPAFPRACHGLOES.Contains("R") || NFPAFPRACHGLOES.Contains("A"))
    {
        NFPAFPRACHGLOES += "/";
    }

    NFPAFPRACHGLOES += "CH";
}

NFPAFPRACHGLOES += "@"; //<--This is where the @ symbol gets added.

if (GBtn == "on")
{
    NFPAFPRACHGLOES += "G";
}

if (LBtn == "on")
{
    NFPAFPRACHGLOES += "L";
}

if (OBtn == "on")
{
    NFPAFPRACHGLOES += "O";
}

if (EBtn == "on")
{
    NFPAFPRACHGLOES += "E";
}

if (SBtn == "on")
{
    NFPAFPRACHGLOES += "S";
}

//NFPAFPRACHGLOES = NFPAFPRACHGLOES.Trim();

NFPAFPRACHGLOES.TrimEnd('\n');
<input type="hidden" id="dumb1" value="@NFPAFPRACHGLOES" /> <-- read value before trimend call
NFPAFPRACHGLOES.TrimEnd('@');
<input type="hidden" id="dumb2" value="@NFPAFPRACHGLOES" /> <-- read value after trimend call.

Here is what I get read out to me in a alert message after it gets back from the AJAX call.

enter image description here

I would try and pass in the ascii encoded version of the character just to see what happens, but as you know, the .TrimEnd() method only accepts char values (and not strings).

I'd use replace, but it must be only if it's at the end of the string, and only it that last character is an @ symbol.

Upvotes: 0

Views: 190

Answers (2)

itsme86
itsme86

Reputation: 19496

string.TrimEnd() doesn't modify the string, but rather creates a new string with the reflected changes. Strings in C# are immutable so this is expected behavior and other string methods work similarly.

Try this:

NFPAFPRACHGLOES = NFPAFPRACHGLOES.TrimEnd('@');

Upvotes: 5

Xavier J
Xavier J

Reputation: 4624

TrimEnd is a function that returns the changed value. You aren't actually assigning the trimmed string value (back to a variable) in your code as-is.

Change your code to:

NFPAFPRACHGLOES = NFPAFPRACHGLOES.TrimEnd('\n')

Upvotes: 7

Related Questions