-1

Yes this post looks a lot like many other posts

It is not. Here is why: I have a string::[email protected] PRIVMSG #lirik :A Message I need to get the username. I need to get everything after position 0 and stop right before the FIRST !

How can I do this?

(Lang: C#, Program: Visual Studio 2015 Community)

7
  • There's no 0 in your string ... Or did you mean the position?
    – Glorfindel
    Aug 13, 2015 at 17:47
  • @Glorfindel Position. My bad. Should be more clear. Edited Post Aug 13, 2015 at 17:47
  • 3
    In any case, look at this question which is basically similar to yours.
    – Glorfindel
    Aug 13, 2015 at 17:48
  • 1
    Or string.split('!')[0]
    – Luc
    Aug 13, 2015 at 17:49
  • 4
    Yes this post looks a lot like many other posts You mean it lacks any evidence that you tried to solve your issue yourself?
    – user1228
    Aug 13, 2015 at 17:50

1 Answer 1

6
var text = ":[email protected] PRIVMSG #lirik :A Message";
var pos = text.IndexOf('!');
var subText = text.Substring(1, pos - 1);

Text to print/display: subText

5
  • 1
    The OP says everything after position 0, so it should be Substring(1, pos - 1);
    – juharr
    Aug 13, 2015 at 17:52
  • @juharr, good call. Thank you. Fixed.
    – Roman
    Aug 13, 2015 at 17:53
  • 1
    @Roman: You will need pos - 1 in the Substring now too.
    – Cᴏʀʏ
    Aug 13, 2015 at 17:53
  • Worked Perfect. I have to wait 6 minutes to accept it. lol. Thanks Aug 13, 2015 at 17:55
  • 1
    Probably should also use IndexOf('!', 1); as well to skip looking at the first position, but wouldn't make a difference for the OP's example. And a check if pos is negative (meaning that ! was not found).
    – juharr
    Aug 13, 2015 at 17:55

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