locked
how to remove part of string RRS feed

  • Question

  • i have funation return to me the username of who loging to system but it is return the user like that "123#wael.fikri" how can i remove this number and # from the string , this # is alowis return befour the name but the numbers it can be 4 digits or less or more but alowis befour the #

    Thanks for any help :) my blog is: http://www.waelk.com

    Monday, May 21, 2012 12:53 PM

Answers

  • Use Remove and IndexOf methods of String class:

     string str = "123#wael.fikri";
     str = str.Remove(0, str.IndexOf('#') +1);

    This will remove the n number infront of # char including it.


    Mitja



    • Proposed as answer by Nico Boey Monday, May 21, 2012 1:03 PM
    • Edited by Mitja Bonca Monday, May 21, 2012 1:03 PM
    • Marked as answer by Lisa Zhu Wednesday, May 23, 2012 3:14 AM
    Monday, May 21, 2012 1:02 PM
  • Note also, if the string was as you wrote by some "typo", my replacement would still correct it. Or we could do this to the pattern.

    String str = "123#wael.fikri123#";
    str = Regex.Replace(str, @"^\d*#", "");

    That would fix the problem you mentioned.


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/


    • Edited by JohnGrove Monday, May 21, 2012 3:37 PM
    • Marked as answer by Lisa Zhu Wednesday, May 23, 2012 3:15 AM
    Monday, May 21, 2012 3:35 PM

All replies

  • Use Remove and IndexOf methods of String class:

     string str = "123#wael.fikri";
     str = str.Remove(0, str.IndexOf('#') +1);

    This will remove the n number infront of # char including it.


    Mitja



    • Proposed as answer by Nico Boey Monday, May 21, 2012 1:03 PM
    • Edited by Mitja Bonca Monday, May 21, 2012 1:03 PM
    • Marked as answer by Lisa Zhu Wednesday, May 23, 2012 3:14 AM
    Monday, May 21, 2012 1:02 PM
  • Hi, 

    You wanted to exclude # as well right?

    Just extending Mitja's solution

    string str = "123#wael.fikri";

    str = str.Remove(0, str.IndexOf('#')+1);



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, May 21, 2012 1:05 PM
  • Or alternatively:

    String str = "123#wael.fikri";
    str = Regex.Replace(str, @"\d+#", "");


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

    Monday, May 21, 2012 3:22 PM
  • Or alternatively:

    String str = "123#wael.fikri";
    str = Regex.Replace(str, @"\d+#", "");


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

    This would behave differently if the string was "123#wael.fikri123#".

    Personally I'd use substring over remove, but here they're not particularly different.

     string str = "123#wael.fikri";
     str = str.Substring(str.IndexOf('#') +1);
    Monday, May 21, 2012 3:27 PM
  • All we have is the data the user provided, and it doesn't give the slightest inkling that his data was like what you jotted down.

    Also, I was providing an alternative not a replacement for the solution that Mitja provided.


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

    Monday, May 21, 2012 3:33 PM
  • Note also, if the string was as you wrote by some "typo", my replacement would still correct it. Or we could do this to the pattern.

    String str = "123#wael.fikri123#";
    str = Regex.Replace(str, @"^\d*#", "");

    That would fix the problem you mentioned.


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/


    • Edited by JohnGrove Monday, May 21, 2012 3:37 PM
    • Marked as answer by Lisa Zhu Wednesday, May 23, 2012 3:15 AM
    Monday, May 21, 2012 3:35 PM