[SOLVED]Printing a portion of a char string.

Is there a simple method to print a portion of a char string? Like from the the 3 element to the end?
I want to be able to point at any given element in the string and then have it print the rest of the string.

I have searched the forums as well as reviewed the string and String reference areas and don't see what I am looking for, but it is possible I missed it.

The way the print function works for char* is that it takes in a char* pointing to the first character in the string, and then keeps writing characters until it reaches a 0. Therefore if you have a string like: "hello!", in the ram this would be:

char str[]={'h','e','l','l','o','!',0}; // completely equivalent to : char* str="hello!";

And your variable, str, would point to the 'h' at the beginning. If you want to read from the third character in (the first 'l') you could do:

print(str+2);

Because str+2 gives you a pointer two elements along from the address pointed to by string.
To get the last 3 characters, you need to obviously first know how long the string is. In some cases you may already know this, so it becomes trivial. If you don't know the position, you can always make a little function that will loop until it finds a 0, counting the number of characters as it goes.

Hope this goes some way to answering your question

Tobyb121

As a very minor change to the approach that toby121 outlined, you can also use array notation to access characters within the string (which is in fact an array).

The array name str is actually equivalent to the address of the first character in the array i.e. &str[0]. The address of the second character is &str[1] - and so on. Note that with any of these techniques, the compiler won't stop you referring to memory locations which are past the end of the array, or within the array but after the null terminator. Passing any of those addresses to a function expecting a string would produce undefined behaviour.

void loop()
{
    char str[20] = "hello";

    Serial.println(&str[1]); // prints "ello"

    Serial.println(&str[10]); // trying to print memory which is after the end of the string, results undefined

    Serial.println(&str[30]); // trying to print memory which is outside the array, results undefined
}

This is loosely referred to as pointer arithmetic, which is often described as "Here be dragons".

Thank you tobyb121, that is exactly what I wanted. (It took me a bit to get all the ' and , in the right spot, but it does what I wanted it to do.

Pete H. I have not tested your code but I understand what you are saying, I will try it as well.

Kerby:
Is there a simple method to print a portion of a char string? Like from the the 3 element to the end?
I want to be able to point at any given element in the string and then have it print the rest of the string.

I have searched the forums as well as reviewed the string and String reference areas and don’t see what I am looking for, but it is possible I missed it.

You can use the substring String function to get part of a string.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
    servo1="";
    servo2="";
  } 
}

zoomkat:
You can use the substring String function to get part of a string.

No you can't.

Arrch:

zoomkat:
You can use the substring String function to get part of a string.

No you can't.

Maybe your complex flavor of string. I consider a string an ordered series of bytes stored in a memory location by various methods, and retrievable and useable by other methods. Instead of beating around the bush, I prefer to get it done.

You can use the substring String function to get part of a string.

No, you can’t. You can use String::substring to get part of a String.

PaulS:

You can use the substring String function to get part of a string.

No, you can’t. You can use String::substring to get part of a String.

Perhaps you are befuddled by the similarity of “string” (string handling functions/operations), “String” (string handling functions/operations), and a “string” (an ordered series of bytes). I send a “string” (an ordered series of bytes) to my arduino via the serial monitor, use “String” functions/operations to extract the “string” (an ordered series of bytes) I’m interested in, and send it back to the serial monitor. Do your double colons compile?

Do your double colons compile?

They most certainly do:

String String::substring( unsigned int left ) const
{
	return substring(left, len);
}

String String::substring(unsigned int left, unsigned int right) const
{
	if (left > right) {
		unsigned int temp = right;
		right = left;
		left = temp;
	}
	String out;
	if (left > len) return out;
	if (right > len) right = len;
	char temp = buffer[right];  // save the replaced character
	buffer[right] = '\0';	
	out = buffer + left;  // pointer arithmetic
	buffer[right] = temp;  //restore character
	return out;
}

Perhaps you are befuddled by the similarity of "string" (string handling functions/operations), "String" (string handling functions/operations), and a "string" (an ordered series of bytes).

You are the befuddled one, constantly using string when you mean String.