Copy link to clipboard
Copied
Could anybody help me with this.
I have a various strings that should (unless it is passed incorrectly which is possible) contain a number at the end, but may also have numbers at the start and/orwithin the string .
Example:
I need to split it so that I end up with the number at the end, so in the examples above the 4567
I could probably do it with a loop but I'm guessing there is a slicker more efficient way of doing it
Thanks
It's not the numbers to the left that I need to get, it's everything left of the numbers on the right.
By @ACS LLC
Then try,
<cfoutput>
<p>
String: 123test456<br>
Left side:#getStartString("123test456")#<br>
Right side: #getEndDigitsFromString("123test456")#
</p>
<p>
String: test456<br>
Left side:#getStartString("test456")#<br>
Right side: #getEndDigitsFromString("test456")#
</p>
</cfoutput>
<cfscript>
public string function getStartString(testString) {
var startString=REReplaceN
...
Copy link to clipboard
Copied
Two suggestions that immediately come to mind:
reverse(val(reverse("123some123string4567")))
Disadvantages: inefficient (3 function calls); suboptimal (returns 0 when there is no number at the end of the string).
REMatchNoCase("\d+$","123some123string4567")
In fact, you could just convert that into a function (for reuse, hence more efficiency):<cfscript>
public string function getEndDigitsFromString(testString) {
/* Default is "" (where there are no end-digits) */
var endDigits="";
/* Regular expression to match the number at the end of the string */
var arrayOfMatches=REMatchNoCase("\d+$",arguments.testString);
if (arrayLen(arrayOfMatches) gt 0) {
endDigits=arrayOfMatches[1];
}
return endDigits;
}
</cfscript>
Example to illustrate using the function:
<cfset myString="123some123string4567">
<cfoutput>
String:#myString#
<br>
Digit(s) from end of string: #getEndDigitsFromString(myString)#
</cfoutput>
Copy link to clipboard
Copied
Thanks @bkbkbk that's exactly what I needed.
I ran a few tests to see if I could trip it up and it works flawlessly
<CFLOOP list="somestring4567,some123string4567,123some123string4567,123somestring4567,123somestring,1234567,123_4567,123@4567" index="myString">
<cfoutput>
String:#myString#
<br>
<b>Digit(s) from end of string: #getEndDigitsFromString(myString)#</b>
</cfoutput>
<br><br>
</CFLOOP>
Copy link to clipboard
Copied
Actually @BKBK Could you possibly help with taking this a little futher, so that I can retrieve not just the numbers at the end, but the other part to the left as separate values, so I've basically split it into two variables. THANKS!
Copy link to clipboard
Copied
I could slice it up with the following, but I'm wondering if you have anything that can go in the CFSCRIPT that would be a little more elequent
<CFSET Digits = #getEndDigitsFromString(myString)#>
<b>Digit(s) from end of string: #digits#</b>
<br><br>
Left side = #left(myString,(len(mystring)-(len(digits))))#
Copy link to clipboard
Copied
I could slice it up with the following, but I'm wondering if you have anything that can go in the CFSCRIPT that would be a little more elequent
By @ACS LLC
Sure. Just implement another function, getStartDigitsFromString():
<cfset myString="987some123string4567">
<cfoutput>
String:#myString#
<br>
Digit(s) at start of string: #getStartDigitsFromString(myString)#
</cfoutput>
<cfscript>
public string function getStartDigitsFromString(testString) {
/* Default is "" (where there are no start-digits) */
var startDigits="";
/* Regular expression to match the number at the start of the string */
var arrayOfMatches=REMatchNoCase("^\d+",arguments.testString);
if (arrayLen(arrayOfMatches) gt 0) {
startDigits=arrayOfMatches[1];
}
return startDigits;
}
</cfscript>
Copy link to clipboard
Copied
Thanks again.
Unfortunately it did not work for the left side. It's giving an empty string, (tested using sdfsdfsdf3423)
Copy link to clipboard
Copied
@ACS LLC , those are the requirements as you gave them. You also confirmed that the solution works.
The first function picks up the number at the end of the string, if there is one. If there is none, it returns an empty string.
The second function picks up the number at the start of the string, if there is one. If there is none, it returns an empty string.
Hence, the code,
<cfoutput> getStartDigitsFromString("sdfsdfsdf3423"):#getStartDigitsFromString("sdfsdfsdf3423")#<br>
getEndDigitsFromString("sdfsdfsdf3423"):#getEndDigitsFromString("sdfsdfsdf3423")#
</cfoutput>
has output:
getStartDigitsFromString("sdfsdfsdf3423"):
getEndDigitsFromString("sdfsdfsdf3423"):3423
which is as expected.
The problem now seems to be that you are adding extra requirements as we go along. Could you therefore say what exactly you require.
Copy link to clipboard
Copied
Sorry I was not as clear as I could have been.
What I am trying to do is get the numbers on the right of the string (that works in the cfscript), but the second part is to get absolutely everything else to the left of those numbers, which is what this was doing
Left side = #left(myString,(len(mystring)-(len(digits))))#
Copy link to clipboard
Copied
The function,
getStartDigitsFromString
gets the numbers to the left.
The function,
getEndDigitsFromString
gets the numbers to the right
Copy link to clipboard
Copied
It's not the numbers to the left that I need to get, it's everything left of the numbers on the right.
Examples:
123test456
Left side: 123test
Right side: 456
test456
Left side: test
Right side: 456
Copy link to clipboard
Copied
It's not the numbers to the left that I need to get, it's everything left of the numbers on the right.
By @ACS LLC
Then try,
<cfoutput>
<p>
String: 123test456<br>
Left side:#getStartString("123test456")#<br>
Right side: #getEndDigitsFromString("123test456")#
</p>
<p>
String: test456<br>
Left side:#getStartString("test456")#<br>
Right side: #getEndDigitsFromString("test456")#
</p>
</cfoutput>
<cfscript>
public string function getStartString(testString) {
var startString=REReplaceNoCase(arguments.testString,"\d+$","");
return startString;
}
public string function getEndDigitsFromString(testString) {
/* Default is "" (where there are no end-digits) */
var endDigits="";
/* Regular expression to match the number at the end of the string */
var arrayOfMatches=REMatchNoCase("\d+$",arguments.testString);
if (arrayLen(arrayOfMatches) gt 0) {
endDigits=arrayOfMatches[1];
}
return endDigits;
}
</cfscript>
Copy link to clipboard
Copied
PERFECT. Thanks for very much for your help with this one. Sorry I didn't explain myself clearly enough at the start.