0

Not sure what I'm doing wrong, the string in question is:

  Type    Family,   Strategy       

I store this in a variable called item, and call item.Trim() however the output is unchanged. Here is the code for my whole function:

private bool checkFeatureList(string item, string feature, bool found)
    {

        //Only match if the feature is the first word TO DO
        if (item.Contains(feature) && found == false)
        {
            int featureLength = feature.Length - 1;
            item.Trim();
            if (item.Substring(0, featureLength) == feature)
            {
                //Have not found the type yet, so add it to the array
                found = true; //Only need the first match
                //feature = item; //Split on double space TO DO
                cleanFeatureList.Add(item);
            }
        }

        return found;
    }

My goal is to add "item" to my array only if the first word matches "feature". The bit about "featureLength" is only to get the first word, this isn't working because my string still has leading spaces after calling item.Trim().

In the example above, I am passing in item as I indicated above, "feature" is "Type" and "found" is false.

user9544908
  • 11
  • 1
  • 5
  • 1
    You're calling `Trim` but ignoring the return value. Strings are immutable - methods like `Trim` and `Replace` don't change the string you call them on - they return the "changed" string. You want `item = item.Trim();`. – Jon Skeet May 19 '19 at 16:01
  • (Not adding this as an answer as I'm sure it's a duplicate - I just need to find it.) – Jon Skeet May 19 '19 at 16:01
  • Hmm... after various searches, I can't find anything. Might as well post an answer. – Jon Skeet May 19 '19 at 16:03
  • @JonSkeet Does `.Trim()` remove &nbsp, looks like you need to use regex or an htmlparser to handle the &nbsp (https://stackoverflow.com/questions/19523913/remove-html-tags-from-string-including-nbsp-in-c-sharp) – Ryan Wilson May 19 '19 at 16:03
  • @RyanWilson: More changes will be required after that, yes - but the answer to the "why doesn't Trim do anything" part is in not using `Trim` properly. – Jon Skeet May 19 '19 at 16:04
  • @JonSkeet I agreed with your explanation, I just wanted to clarify if calling .Trim would do what the OP wants, thanks Jon. – Ryan Wilson May 19 '19 at 16:06
  • @NatPongjardenlarp: I saw that, but I'm not sure it's quite suitable for this question where the OP already knows about Trim. As a purely personal opinion, I would prefer to see a different answer, even though the one there does explain it. – Jon Skeet May 19 '19 at 16:06

1 Answers1

2

This is your current call to Trim:

item.Trim();

The Trim method doesn't change the content of the string you're calling on. It can't - strings are immutable. Instead, it returns a reference to a new string with the trimming applied. So you want:

item = item.Trim();

Note that you'll still need additional string manipulation to handle   appropriately, but this will at least trim whitespace from the start and end of the string as you want.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194