Results 1 to 6 of 6

Thread: Please help me with this simple string modification

  1. #1
    Join Date
    Jul 2007
    Posts
    70
    Mentioned
    0 Post(s)
    Quoted
    25 Post(s)

    Default Please help me with this simple string modification

    So I am making a slayer bot on a rsps, I grab the task from the player interface. However it returns tasks like 23 hellhounds. I only want to return hellhounds, how do I get rid of the number's before the string I want? Also some times the task could be a 2 word monster like iron dragons, or simple 1 word like hellhound. There is always a number returned with gettextatex with the method I am using, but I need it to only be the monster name. Thanks in advance.

  2. #2
    Join Date
    Dec 2010
    Posts
    483
    Mentioned
    30 Post(s)
    Quoted
    328 Post(s)

    Default

    Simba Code:
    function GetMonsterName(text: String): String;
    begin
      result := Copy(text, Pos(' ', text) + 1, Length(text) - Pos(' ', text) + 1);
    end;

    Code:
    WriteLn(GetMonsterName('40 Hellhounds'));
    Outputs: Hellhounds
    
    WriteLn(GetMonsterName('40 Iron Dragons'));
    Outputs: Iron Dragons
    Best practice would include sanity checks on input, but then it wouldn't be a pretty one-liner. Something like this:

    Simba Code:
    function GetMonsterName(text: String): String;
    begin
      if Pos(' ', text) = 0 then
        RaiseException(erCustomError, 'GetMonsterName(): invalid string passed to function');

      result := Copy(text, Pos(' ', text) + 1, Length(text) - Pos(' ', text) + 1);
    end;

    You could then catch the exception and handle it, such as:

    Simba Code:
    begin
      try
        WriteLn(GetMonsterName('fasdfasegasrdg'));
      except
        WriteLn('lorem ipsum'); //handle your exception here, maybe try to re-fetch it?
      end;
    end.

    Or just let it error out.

  3. #3
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    This is the function I linked you when you asked me: http://docs.villavu.com/simba/script...extractfromstr
    Just do
    Simba Code:
    ExtractFromString('44 monsters', LETTERS);

  4. #4
    Join Date
    Dec 2010
    Posts
    483
    Mentioned
    30 Post(s)
    Quoted
    328 Post(s)

    Default

    Quote Originally Posted by Citrus View Post
    This is the function I linked you when you asked me: http://docs.villavu.com/simba/script...extractfromstr
    Just do
    Simba Code:
    ExtractFromString('44 monsters', LETTERS);
    Would that work with monsters such as 'Kal'gerion Demons'? What about monsters with spaces, such as 'Iron Dragons'?

    Either way I didn't know this function existed, that's pretty neat.

    Further info:
    Code:
    StrExtr = (Numbers, Letters, Others);

  5. #5
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Code:
    ReplaceRegExpr('\d+ ', input, '', false)
    Code:
    program test;
    var s:string;
    begin
      for s in ["12 Kal'gerion Demon", '11 test', '0 booger eaters'] do
      begin
        writeln(ReplaceRegExpr('\d+ ', s, '', false));
      end;
    end.

  6. #6
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    Quote Originally Posted by the bank View Post
    Would that work with monsters such as 'Kal'gerion Demons'? What about monsters with spaces, such as 'Iron Dragons'?

    Either way I didn't know this function existed, that's pretty neat.

    Further info:
    Code:
    StrExtr = (Numbers, Letters, Others);
    Yeah, good point. Considering those cases, it's probably best to loop through each character in the string and copy everything after, and including, the first letter. That covers cases without numbers too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •