cancel
Showing results for 
Search instead for 
Did you mean: 

Count characters from end of a string to front

SOLVED
Occasional Contributor

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.

 

 

Tags (2)
3 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Count characters from end of a string to front

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);
}

 

Community Leader

Re: Count characters from end of a string to front

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 ($) 

 

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,

Re: Count characters from end of a string to front

 

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;
}
6 REPLIES 6

Re: Count characters from end of a string to front

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);
}

 

Community Hero

Re: Count characters from end of a string to front

Hi,

 

aqString.FindLast() ?

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Leader

Re: Count characters from end of a string to front

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 ($) 

 

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,

Re: Count characters from end of a string to front

 

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;
}
Frequent Contributor

Re: Count characters from end of a string to front

@mioani 

 

Please use the below code and let me know. 


Sub test1
str = "HELLO"
no_of_Char_ToFind = "E"
call findCharFromLast(str,no_of_Char_ToFind)
End Sub

 

Sub findCharFromLast(str,no_of_char_ToFind)
strLen = aqstring.GetLength(str)
for i = strLen-1 to 0 step -1
if aqstring.find(aqString.GetChar(str,i), no_of_char_ToFind) <> -1 then
exit for
else
temp = temp& aqstring.GetChar(str,i)
end if
next
log.Checkpoint aqstring.getLength(temp)
End Sub

 

 

 

 

'To input string and no. of characters needed

Sub findCharFromLast(str,no_of_char_ToFind)
strLen = aqstring.GetLength(str)
for i = strLen-1 to no_of_Char_ToFind-1 step -1
temp = temp& aqstring.GetChar(str,i)
next
log.Checkpoint aqstring.getLength(temp)
End Sub

 

Sub test1
str = "HELLO"
no_of_Char_ToFind = 3
call findCharFromLast(str,no_of_Char_ToFind)
End Sub

 

Best Regards,

Subbulakshmi S

 

Regards,
Subbulakshmi
Highlighted
Community Hero

Re: Count characters from end of a string to front

VBScript has InStrRev() built-in function (https://www.w3schools.com/asp/func_instrrev.asp)...

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video:
Top Kudoed Authors