Mechatronics LAB
Published © GPL3+

Arduino and MicroPython String Functionality

In this section, you will learn how to split a string containing comma-separated values into individual parts using both Arduino and MicroPy

BeginnerProtip1 hour62
Arduino and MicroPython String Functionality

Things used in this project

Hardware components

ESP32 Development Board
×1
USB Cable
×1
Breadboard (optional)
×1
Jumper Wires
×1

Story

Read more

Code

Code -1

Arduino
// Arduino code to split a comma-separated string
String text = "Sarful,Lima,Rowsoni";  // an example string
String message = text; // holds text not yet split
int commaPosition;  // the position of the next comma in the string

void setup()
{
  Serial.begin(9600);
  while(!Serial); // Wait for serial port (Leonardo, 32-bit boards)

  Serial.println(message); // show the source string
  do
  {
    commaPosition = message.indexOf(',');
    if(commaPosition != -1)
    {
      Serial.println( message.substring(0,commaPosition));
      message = message.substring(commaPosition+1, message.length());
    }
    else
    { // here after the last comma is found
      if(message.length() > 0)
        Serial.println(message);  // if there is text after the last comma,
                                  // print it
    }
   }
   while(commaPosition >=0);
}

void loop()
{
  // Empty loop, no functionality needed here
}

Code -2

Python
# MicroPython code to split a comma-separated string

text = "Sarful,Lima,Rowsoni"  # an example string
message = text  # holds text not yet split

while ',' in message:
    part, _, message = message.partition(',')
    print(part)
if message:  # print the last part if any
    print(message)

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.