Forum Discussion

mioani's avatar
mioani
Occasional Contributor
6 years ago

Count characters from end of a string to front

Hi guys.

 

Is there any option to count the characters in a string to a specific length from back to front, for example I have a string that says Hello but want to count the characters up to the letter "e", meaning "llo" that equals 3. Which method should this be. 

 

Thank you.

 

 

  • Hi
    You can use this code:

    function Test()
    {
      var str = "Hello"
      var startPos = aqString.Find(str, "e");
      var result = aqString.GetLength(str) - startPos - 1;
      Log.Message(result)
    }

    or (for JavaScript)

    function Test()
    {
      var str = "Hello"
      var patt = new RegExp('e' + '.*$');
      var result = aqString.GetLength(str.match(patt)) - 1
      Log.Message(result);
    }

     

  • This will work if your word has only one "e" in or you want to find everything following the first "e"

    e.g. "Here we go"

     

    would return "re we go"

     

    if you want to find the last one though, you need a 

     

    aqString.FindLast("Here we go", "e", true);  which should return " go"

     

    With javescript the regex searches from the end ($) 

     

     

  • IStaroverov's avatar
    IStaroverov
    6 years ago

     

    For some characters:

    function Test()
    {
      var results = getCount("ere we ge", "e")
      for(result of results) 
        Log.Message(result)
    }
    
    function getCount(str, char)
    {
      var startIndex = 0;
      var result =[];
      var startPos = aqString.Find(str, char, startIndex);
      while(startPos != -1)
      {
        result.push(aqString.GetLength(str) - startPos - 1);
        startIndex = startPos + 1;
        startPos = aqString.Find(str, char, startIndex);
      }
      return result;
    }
  • IStaroverov's avatar
    IStaroverov
    SmartBear Alumni (Retired)

    Hi
    You can use this code:

    function Test()
    {
      var str = "Hello"
      var startPos = aqString.Find(str, "e");
      var result = aqString.GetLength(str) - startPos - 1;
      Log.Message(result)
    }

    or (for JavaScript)

    function Test()
    {
      var str = "Hello"
      var patt = new RegExp('e' + '.*$');
      var result = aqString.GetLength(str.match(patt)) - 1
      Log.Message(result);
    }

     

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      This will work if your word has only one "e" in or you want to find everything following the first "e"

      e.g. "Here we go"

       

      would return "re we go"

       

      if you want to find the last one though, you need a 

       

      aqString.FindLast("Here we go", "e", true);  which should return " go"

       

      With javescript the regex searches from the end ($) 

       

       

      • IStaroverov's avatar
        IStaroverov
        SmartBear Alumni (Retired)

         

        For some characters:

        function Test()
        {
          var results = getCount("ere we ge", "e")
          for(result of results) 
            Log.Message(result)
        }
        
        function getCount(str, char)
        {
          var startIndex = 0;
          var result =[];
          var startPos = aqString.Find(str, char, startIndex);
          while(startPos != -1)
          {
            result.push(aqString.GetLength(str) - startPos - 1);
            startIndex = startPos + 1;
            startPos = aqString.Find(str, char, startIndex);
          }
          return result;
        }