1

Let's say I have a string:

string str = "09012013 Receipt 09012013 #12"

I want to do substring to return "Receipt 09012013 #12", I used substring:

var result = str.Substring(9);

and the result was only "Receipt 09012013"

I tried with other special characters (%,§,$ ...), it worked, substring returned "Receipt 09012013 %12", but with # and &, substring only returned "Receipt 09012013".

Any thoughts? Thanks.

EDIT

My code:

new NaviListItem("renameBtn", "showwaitingscreen", "akte/renameakte?entityid=" + Request["parentid"] + "&aktenkurzbezeichnung=" + Model.Node.Header.Substring(Model.Node.Ordnungsnummer.Length+1), "umbenennen.png", Model.RenameVisible, "Umbenennen", "Umbenennen"),
5
  • 4
    For me result is "Receipt 09012013 #12". Have you wriiten your own extension method Substring? Commented Jan 9, 2013 at 14:07
  • It retursn ok on my computer for every special character in your question. Commented Jan 9, 2013 at 14:07
  • How do you know your result is "Receipt 09012013"? Try MessageBox.Show(result)
    – Bolu
    Commented Jan 9, 2013 at 14:11
  • @HamletHakobyan: I used standard Substring. I used this in View.cshtml, to pass the value to JQueryAjaxDialog
    – Ragnarsson
    Commented Jan 9, 2013 at 14:13
  • Then you must follow @dasblinkenlight recommendations. Commented Jan 9, 2013 at 14:14

1 Answer 1

6

The result of Substring does not depend on the characters at the end:

string str = "09012013 Receipt 09012013 #12".Substring(9);

produces "Receipt 09012013 #12" as its result.

Most likely this is a display issue: if you are delivering the result of the Substring over some sort of HTML-enabled display mechanism, the & and # would often be treated as meta-characters, and therefore require escaping.

3
  • Yes, I used it in a View.cshtml, to pass value to JQueryAjaxDialog, so, do you have solution ?
    – Ragnarsson
    Commented Jan 9, 2013 at 14:15
  • 2
    @QuiTran "and therefore require escaping." Commented Jan 9, 2013 at 14:20
  • 2
    @QuiTran Use System.Web.HttpUtility.HtmlEncode() on the result of Substring to escape meta-characters. Commented Jan 9, 2013 at 14:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.