You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to replace / substitute THE LAST OCCURRENCE of a character in a string ?

Hello,


I’m in NUMBERS 13.1 on MacOS 12.6.6 (but it’s not a version issue).


I have several values with different lengths in column B, e. g.: B6 : 56:01:59

And I want to REPLACE or SUBSTITUTE THE LAST OCCURRENCE of ":" by "," in order to return the following value instead in C6 : 56:01,59

i. e. : having the last digits after the comma identified as milliseconds.


I tested different formulas in column C. e.g. : C6 : REPLACE(B6;”:";",";2)

It’s returning 56:01,59 which is correct, but wouldn’t work with a longer string ( WRONG 00:00,00:00 ).

Select-Replacing the FIRST OCCURRENCE (start counting from LEFT) seems easier than Select-Replacing THE LAST OCCURRENCE of the ":".


How can I Select-Replace THE LAST OCCURENCE of ":" in a formula for cells with different string-lengths ? and

How would a nested Formula look like, using REPLACE/SUBSTITUTE and RIGHT, in oder to invert the count-direction ?


2023-11-02_TABLE-EXCERPT_REPLACE_FROM_RIGHT_OR_LAST_OCCURRENCE-ISSUE.53.1.png


/ How does the NUMBERS formula look like, assuming 
working with either :


a) a « REPLACE Last Occurrence Of ”:” » approach, 
or


b) a « REPLACE First Occurrence from RIGHT » approach

?


Any help much appreciated


:)

Posted on Nov 2, 2023 9:54 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 2, 2023 11:05 AM

If all the source string is guaranteed to have at least one ":" and if that ":" will always be followed by exactly 2 additional characters (as in 6:21 or 6:21:32 but not 6:213 or 6213),

=REPLACE(B2,LEN(B2)−2,1,",")


Otherwise, this one will replace the last ":" no matter where it is in the string

=IFERROR(SUBSTITUTE(B2,":",",",COUNTMATCHES(B2,":")),B2)


The IFERROR function is there is for when the string does not have a ":". There is probably a better formula but these two appear to do what you requested.




18 replies
Question marked as Top-ranking reply

Nov 2, 2023 11:05 AM in response to gestyle

If all the source string is guaranteed to have at least one ":" and if that ":" will always be followed by exactly 2 additional characters (as in 6:21 or 6:21:32 but not 6:213 or 6213),

=REPLACE(B2,LEN(B2)−2,1,",")


Otherwise, this one will replace the last ":" no matter where it is in the string

=IFERROR(SUBSTITUTE(B2,":",",",COUNTMATCHES(B2,":")),B2)


The IFERROR function is there is for when the string does not have a ":". There is probably a better formula but these two appear to do what you requested.




Nov 2, 2023 12:01 PM in response to Badunit

Thanks a lot for your message, Badunit


Is there perhaps any dependency with the source-cell-format, which in automatic mode turns into a date ?


For that reason, instead of a «text-field» or an «automated» Data Format, I attempted to create a custom-digits-format, which currently still seems to bug.. :P


( 2023-11-02_TABLE-EXCERPT_REPLACE_FROM_RIGHT_OR_LAST_OCCURRENCE-ISSUE.53.2.png )


( 2023-11-02_TABLE-EXCERPT_REPLACE_FROM_RIGHT_OR_LAST_OCCURRENCE-ISSUE.53.3.png )


But even when selected, it doesn’t lock and remains a «text» … Not sure why..

( 2023-11-02_TABLE-EXCERPT_REPLACE_FROM_RIGHT_OR_LAST_OCCURRENCE-ISSUE.53.4.png )


So, it seems to currently affect both formulas. Do you perhaps see what’s happening ?


:)

Nov 3, 2023 10:33 AM in response to gestyle

gestyle, be mindful of the illusions. Values are not always what they appear to be. Comma and decimal point are not always interchangeable depending on the language. Even divising the value by 1 is hit-and-miss, I dropped it for that reason.


The small table Décimal provides the valid decimal separator in all circumstances. Value 1.0 in A1, MID(A1,2,1) in A2.

Nov 6, 2023 4:41 PM in response to gestyle

I came across another issue among the values of my chart : some values got interpreted 00:24:18,00 instead of 00:00:24,18. While others get correctly interpreted : start-value : 0:17:73 … Correct result : 0:17,73

I’m wondering if there is any trick able to fix the issue with those longer strings ending with 00 –?–


( 2023-11-07_TABLE-EXCERPT_REPLACE_FROM_RIGHT_OR_LAST_OCCURRENCE-ISSUE.53.1.png )

Nov 7, 2023 2:12 AM in response to gestyle

Hi gestyle,


Back to the title of your original post:

gestyle wrote: How to replace / substitute THE LAST OCCURRENCE of a character in a string


The functions TEXTBEFORE and TEXTAFTER are your friends.



-1 will find the last occurrence of ":"

&","& will insert a comma. Assuming that your Language & Region is France, that will represent a decimal separator. But the result will be Text, not minutes:seconds:milliseconds.

It will represent minutes, seconds and decimal fractions of seconds.


Good luck with your project.

Regards,

Ian.



Nov 7, 2023 3:15 AM in response to gestyle

This will ignore what is after the two characters following the last colon.


=TEXTBEFORE("0:"&$B47,":",−1)÷1+DURATION(,,,,LEFT(TEXTAFTER("0:"&$B47,":",−1),2)÷100)



Ian, the reason I use TEXTBEFORE("0:"&$B47, etc. is that without the "0:"& part a value like 23:45 would only return 23, which creates an error when trying to convert it to a duration. It might be overkill in this case if data is guaranteed to always have at least three parts but I don't take any chance (also I write my answers thinking of all future readers who might have a similar but not quite identical situation).


Interesting tidbit I discovered while doing this project. A value like 2:14:6:3:5 is the same as 2 weeks 14 days 6 hours 3 minutes 5 seconds, a shorthand for function DURATION. Like =B45+"1:6:27:0". It might be useful sometimes.

Nov 9, 2023 6:33 PM in response to Recycleur

Thanks again to you @Recycleur,


I made a tiny syntax-adaptation for the formula to be valid in my QWERTZ machine/environment – I think this is important for others to stress – see screenshot.


Now while the formula gives a coherent output on short strings (the 3-blocks-strings), it’s still kind of swaping the duration-units when processing long strings (the 4-blocks-strings) :


Do you see any way how to also fix this point ?


:)

Nov 2, 2023 12:59 PM in response to gestyle

My source column was pre-formatted as text. The custom format in your screenshot does not match what I see in column B. It has no colons in it, all the numbers run together. I tried a custom date format similar to yours, except with colons between the numbers, and it worked correctly. But I recommend formatting column B as text.

Nov 6, 2023 6:24 PM in response to gestyle

I don't understand the issue. If 0:41:48 is OK to turn into 0:41.48, why is 00:44:48:00 not OK to turn into 0:44:48.00 ? I thought the last two digits after the last colon were always the 100th of a second.


Note that in the top table, you have 00:24:18,00 (with a comma) in the source column while in the bottom table that's not the case. How can the 00 after the comma be understood to go in front? Is it a CSV reading error?


I worked out another formula, not better than the original except that it ignores the comma/point dilemma for the decimal separator.

TEXTBEFORE("0:"&$B38,":",−1)÷1+DURATION(,,,,TEXTAFTER("0:"&$B38,":",−1)÷100)


Oh, and the division by 1 somehow converts a string into a number. You must have seen it in my first answer, which I edited afterwards to remove it. But sometimes it works.

How to replace / substitute THE LAST OCCURRENCE of a character in a string ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.