Skip to main content
Inspiring
August 29, 2023
Answered

Splitting numerics from a string

  • August 29, 2023
  • 1 reply
  • 1292 views

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:

  • somestring4567
  • some123string4567
  • 123some123string4567
  • 123somestring4567

 

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

This topic has been closed for replies.
Correct answer BKBK

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

 


quote

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>

 

 

1 reply

BKBK
Community Expert
Community Expert
August 31, 2023

Two suggestions that immediately come to mind:

  1.  a combination of reverse() and val()
    reverse(val(reverse("123some123string4567")))

    Disadvantages: inefficient (3 function calls); suboptimal (returns 0 when there is no number at the end of the string).

  2.  Better solution: regular expression together with REMatchNoCase(): 
    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>



ACS LLCAuthor
Inspiring
September 6, 2023

Thanks @366678 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>

ACS LLCAuthor
Inspiring
September 6, 2023

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!