• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Splitting numerics from a string

Enthusiast ,
Aug 29, 2023 Aug 29, 2023

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:

  • 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

TOPICS
Advanced techniques

Views

528

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 10, 2023 Sep 10, 2023
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=REReplaceN
...

Votes

Translate

Translate
Community Expert ,
Aug 31, 2023 Aug 31, 2023

Copy link to clipboard

Copied

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>



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 06, 2023 Sep 06, 2023

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 06, 2023 Sep 06, 2023

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 06, 2023 Sep 06, 2023

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))))#

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2023 Sep 08, 2023

Copy link to clipboard

Copied

quote

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>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 10, 2023 Sep 10, 2023

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2023 Sep 10, 2023

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.

 

   

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 10, 2023 Sep 10, 2023

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))))#

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2023 Sep 10, 2023

Copy link to clipboard

Copied

The function, 

getStartDigitsFromString

gets the numbers to the left.

 

The function, 

getEndDigitsFromString

gets the numbers to the right

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 10, 2023 Sep 10, 2023

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2023 Sep 10, 2023

Copy link to clipboard

Copied

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>

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 11, 2023 Sep 11, 2023

Copy link to clipboard

Copied

LATEST

PERFECT. Thanks for very much for your help with this one. Sorry I didn't explain myself clearly enough at the start.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation