Use substring from right

Probaby a stupid question but how to use this fonction but from the right? in some language you just have to use the minus but it seems not to work with the substring.

String test = 12345688

I want to slipt the string from the last 2 digit, so 123456 and 88. The length of the string may vary so that why i want to go from the right so it will alway be the 2 last digit that will be split. I saw on the forum that it could maybe be achived by dtostrf() but didn't saw a good explanation. And for sure i want to keep this in String Thanks

The length of the string may vary

But is not unknown. Use length-2 as the start of the substring.

You can get the length of the string, then get the last two characters.

You are right, thanks for the tips, I use:

int length = myString.length();

To determinate the length and after that it was a piece of cake. Thanks again

Maybe a shorter way without having to get the length separately.

// zoomkat 9-24-13 serial I/O string test
// type a string in serial monitor. then send or enter

String readString, newString;

void setup() {
  Serial.println("serial test 0021"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c =;
    readString += c;

  if (readString.length() >0) {
    newString = readString.substring(readString.length() - 2, readString.length());
1 Like