String "Limiter" function

I would like to find out if there is a function in a library such that if I were to specify the start and end position of a string, it will output that part of the string.

For example,

string= "1234567890abc"
out=string.limiter(0,4) ; //start, stop

result: out="90abc"

string= "1234567890abc"
out=string.limiter(2,9) ; //start, stop

result: out="4567890a"

The below may be what you are looking for.

It is easy to add that functionality in by deriving the string class, however, I do not understand your example:

string= "1234567890abc"
out=string.limiter(0,4) ; //start, stop

result: out="90abc"

'9' is neither the 'zero'th' character from either end of the string, nor is 'c' the fourth, maybe the 4 is a length? Can you explain what your wanted function is actually doing?

  • strncpy() ? -

strncpy(dest, &src[2], 9); // copies from array src to dest, form byte 2 9 bytes max or less if a \0 is reached..

- strncpy() ?  -

strncpy(dest, &src[2], 9); // copies from array src to dest, form byte 2 9 bytes max or less if a \0 is reached..

Can you please explain ?

I need to get part of a string such as "31/12/2013"
How can I go about getting only the part "2013" using strncpy ?

Here is my attempt, but I would value advice on the need to add the trailing NULL to the destination or not.

char source[] = "31/12/2013";
char dest[11];

void setup() 
{
  Serial.begin(115200);
  int len = 4;
  strncpy(dest, &source[6], len);
  dest[len] = '\0';
  Serial.println(dest);
}

void loop() 
{
}

You could, of course, turn this into a function that accepts the start position and length to copy and returns the substring.

NOTE - this uses C style strings (NULL terminated arrays of chars) rather than Strings

If the string to be split has delimiters, as in your example, then you would probably be better off using the strtok() function to split the source string at the backslashes.

Here is a happy fun C++ design:

template< typename T, unsigned N >
  unsigned int InArray( T (&t)[ N ], const T &f, unsigned z = 0x00 )
    {
      for( unsigned i = z ; i < N ; ++i ) if( f == t[ i ] ) return i;
    }

void setup() {
  Serial.begin( 9600 );
  
  char c[] = "31/12/2013";
  char s ='/';
  
  Serial.print( &c[ InArray( c, s, InArray( c, s ) + 1 ) + 1 ] );
}

void loop() {}

tolisn63:
I need to get part of a string such as "31/12/2013"

Also more simple using only the function sscanf()

void setup()
{ char source[] = "31/12/2013";
  int dd,mm,aa;
  delay(1000);
  Serial.begin(9600);
  sscanf(source,"%u/%u/%u",&dd,&mm,&aa);
  Serial.println(source);
  Serial.println(dd);
  Serial.println(mm);
  Serial.println(aa);
}
void loop()
{}

I'm glad that my wife is not into programming because she hates me answering a question from her about how to do something and me starting "Well..." because she knows that I will be giving alternatives and that is not usually what she wants to hear.

So many ways to do the same thing and all of them with advantages and disadvantages. What fun we have giving advice.

Ah, okay @UKHeliBob, only your advice is okay. :grin:
You response to first question about using a String object with a function strncpy() that is not for String object.
I suggest a function to @tolisn63 about another question.

Happy New Year. :slight_smile:

Happy new year,

here is a JQuery inspired design alowing a simple?? syntax like: s.find( '/' ).find( '/', 1 ).next().print( Serial );.
It is an excerpt from a new lib of mine, uncreatively named CQuery

struct Str{
  Str( char* _s ) : s( _s ){}
  
  operator char*() { return this->s; }
  
  Str find( char c, int offset = 0x00 ){
    for( unsigned i = offset ; s[ i ] ; ++i ) if( c == s[ i ] ) return Str( s + i );
    return Str( this->s );
  }
  
  void print( Print &p ) { p.print( s ); }
  Str next( void ) { return Str( ++s ); }
  char *s;
};

void setup() {

  char c[] = "31/12/2013";
  Str s = c;

  Serial.begin( 9600 );
  
  s.find( '/' ).find( '/', 1 ).next().print( Serial );
}

void loop() {}

Returns 2013

I have been designing it for an HTTP server which has to parse responses.