Hello I an new here and I have what should be a simple question.
How do I cut strings with arduino ide ?
I have looked at the examples but am now even more confused.
In the past I have used the BASIC programming language with the
Left$
Mid$
Right$
commands, it cut strings but whats the C code to do the same?
BASIC example if I have the string "123456789"
Left$(3) would give "123" the first 3 characters from the left
Mid$(4,2) would give "45" from the 4th character for 2 characters in length
Right$(4) would give "6789" the 4 characters to the right
Is this simple system not used on the arduino ide?
Thanks to anyone who can help me.
The IDE knows nothing about strings or Strings, but C/C++ knows a lot about both.
gcjr
June 7, 2021, 11:26am
3
there a library with string functions
void fParseLIDAR_ReceivedSerial ( void * parameters )
{
// distribute received LIDAR info
String sTmp = "";
sTmp.reserve ( 20 );
String sMessage = "";
sMessage.reserve ( StringBufferSize300 );
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
int commaIndex = sMessage.indexOf(',');
sTmp.concat ( sMessage.substring(0, commaIndex) );
sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
if ( sTmp == "!" )
{
xSemaphoreGive ( sema_LIDAR_OK );
// Display info from LIDAR
sLIDAR_Display_Info = sMessage;
}
if ( sTmp == "$" )
{
xEventGroupSetBits( eg1, evtResetWatchDogVariables );
}
if ( sTmp == "#")
{
xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
sLIDAR_Alarm_info = sMessage;
xSemaphoreGive( sema_LIDAR_Alarm );
xEventGroupSetBits( eg, evtfLIDAR_Alarm );
}
sTmp = "";
sMessage = "";
xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
}
vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )
I parsed strings with the above task, perhaps a model that you can use.
Take a look at the functions linked from this page String() - Arduino Reference
Expect replies here advising you not to use Strings and to use zero terminated arrays of chars instead
Hi Thats great but unless I am missing something it will not do what I want.
Idahowalker:
void fParseLIDAR_ReceivedSerial ( void * parameters )
{
// distribute received LIDAR info
String sTmp = "";
sTmp.reserve ( 20 );
String sMessage = "";
sMessage.reserve ( StringBufferSize300 );
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
int commaIndex = sMessage.indexOf(',');
sTmp.concat ( sMessage.substring(0, commaIndex) );
sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
if ( sTmp == "!" )
{
xSemaphoreGive ( sema_LIDAR_OK );
// Display info from LIDAR
sLIDAR_Display_Info = sMessage;
}
if ( sTmp == "$" )
{
xEventGroupSetBits( eg1, evtResetWatchDogVariables );
}
if ( sTmp == "#")
{
xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
sLIDAR_Alarm_info = sMessage;
xSemaphoreGive( sema_LIDAR_Alarm );
xEventGroupSetBits( eg, evtfLIDAR_Alarm );
}
sTmp = "";
sMessage = "";
xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
}
vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )
Wow !!!! this seems ott for a simple task,
Thanks for the reply but I find this hard to understand ..
That's right - you're probably missing something.
We certainly are. (That's a hint, BTW)
I never really understood how BASIC made it so complicated.
Boffin
June 7, 2021, 11:52am
9
Take a look here for a start.
substring()
String substring Function
String myString;
myString.substring(0, to) // Left$
myString.substring(from, to) // Mid$
myString.substring(from) // Right$
Read how to set the second index correctly.
gcjr
June 7, 2021, 11:59am
10
?
consider the code below producing the following output
123
4567
90
#include <stdio.h>
#include <string.h>
int
main ()
{
char s [] = "1234567890";
char buf [20];
memset (buf, 0, 20);
strncpy (buf, & s[0], 3);
printf (" %s\n", buf);
memset (buf, 0, 20);
strncpy (buf, & s[3], 4);
printf (" %s\n", buf);
memset (buf, 0, 20);
strncpy (buf, & s[8], 4);
printf (" %s\n", buf);
}
LMI1
June 7, 2021, 12:02pm
11
Yes, Arduino or C has of plenty string commands. People have been asking what you want to do. But you may Google for instance STRCAT C, or simply goto
You will find that parsing Strings will be a pain, which ever way you decide to do the thing.
LMI1
June 8, 2021, 1:04pm
13
Do you mean getting for instance file names and such, out of strings, I believe you. I think Microsoft had something called like regular expressions, I looked at it and it was awfull, not for mortal common people. Arduino should make such lib, but it probably would take whole program memory.
But simple things like strcat and strcpy work.
system
Closed
October 6, 2021, 1:07pm
15
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.