0

I'm a little puzzled on why does the Substring() function not working properly. It just won't chopped off the comma at the end of the string.

See example code below...

public static string OrderByClauseBuilder(string parmSortByColumn)
{
    if (parmSortByColumn.LastIndexOf(",") > -1) { 
        parmSortByColumn.Substring(0, parmSortByColumn.LastIndexOf(","));
    }
    return parmSortByColumn;
}
protected void Page_Load(object sender, EventArgs e)
{
    string sqlAsk = "";
    string value = "stocknumber asc,";

    sqlAsk = OrderByClauseBuilder(value);
}
fletchsod
  • 3,560
  • 7
  • 39
  • 65
  • You could start by fixing LastIndex (to LastIndexOf). – H H Jun 14 '13 at 15:26
  • Oops.. Typo error when posting the question. – fletchsod Jun 14 '13 at 15:28
  • @fletchsod, please consult [the docs](http://msdn.microsoft.com/en-us/library/aka44szs.aspx) for such obvious questions. (In the remarks there, "This method does not modify the value of the current instance. Instead, it returns a new string with length characters starting from the startIndex position in the current string.") – Kirk Woll Jun 14 '13 at 15:33
  • 1
    @fletchsod - I thought so , but you shouldn't be typing the code here. Much better to copy/paste something that has been through a compiler. – H H Jun 14 '13 at 15:38
  • @HenkHolterman - Problem is this Internet computer is on a seperate network. So, it's easier for me to type than copying it, put it on floppy (oh wait no floppy anymore) then do sneakernet then paste. Oh well! – fletchsod Jun 14 '13 at 15:51
  • @HenkHolterman - The reason for Internet computer is to avoid hackers from places like China from stealing source code on our local network as it had already happened to Google and other big businesses. – fletchsod Jun 14 '13 at 15:53
  • All very well but the burden is on you to post accurate questions to prevent people wasting time. – H H Jun 15 '13 at 12:34

1 Answers1

7

Strings are immutable in .NET, and each method of string returns a new, modified one. Therefore you have to do some adjustments:

public static string OrderByClauseBuilder(string parmSortByColumn)
{
    if (parmSortByColumn.LastIndexOf(",") > -1) { 
        parmSortByColumn = parmSortByColumn.Substring(0, parmSortByColumn.LastIndexOf(","));
    }
    return parmSortByColumn;
}
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • 3
    +1. And it would be way easier to generally use `return parmSortByColumn.TrimEnd(',');`, IMO. :) – Patryk Ćwiek Jun 14 '13 at 15:24
  • 1
    @Trustme-I'maDoctor, totally agree. Question was about Substring though. – Andrei Jun 14 '13 at 15:25
  • I know, I know, just pointing it out, maybe OP will change his mind. – Patryk Ćwiek Jun 14 '13 at 15:26
  • 2
    @Trustme-I'maDoctor, actually these two approaches are different. `Trim` won't do anything if last comma is not a last character of the string. It will work in the case OP has posted, but generally speaking they do different things. – Andrei Jun 14 '13 at 15:30
  • Gee! I didn't see that. You're right!! It was as result of not having parmSortByColumn assigned copy of parmSortByColumn. **palm of my hand slapping my head** – fletchsod Jun 14 '13 at 15:32
  • That's true, I was just following the example, although if there's more to this particular method usage than meets the eye, then you already provided fixed version. :) – Patryk Ćwiek Jun 14 '13 at 15:33
  • Aw nice! I'm not aware of TrimEnd() function. Nice!!! Thanks for educating me on this one. :-D – fletchsod Jun 14 '13 at 15:33