0
How to slice a string by using index range in c
5 Antworten
+ 5
Of all the high-level languages I know, C provides the least support for strings. There is not a general way of doing string slicing that I can recommend. Can you give a specific example of what you want to do with the slices? E.g., cut a string into three distinct shorter strings and preserve each part, or extract one substring from the middle, or preserve only two parts, or do you need to leave the original string intact, or is it okay to discard the beginning and end of the original string? Do you just need to print a slice, or will you need to manipulate the string part(s) afterward?
The most powerful tool in the toolbox is sprintf(), but it may be more than is needed for this. There is also the string.h library. The right answer depends on the details.
+ 4
Hi Abinesh M. In this case I recommend using sscanf(). It can read multiple portions of the string while breaking it up into given field widths, and it can store the results into individual strings. Further, it can convert to numeric values instead of storing as strings, if that is useful.
I quickly wrote a demonstration. This should answer most of your questions about how to use sscanf to do the task.
https://code.sololearn.com/cZ6E9uXGEYKl/?ref=app
+ 2
Abinesh M I have not worked with Arduinos. From what I can find, Arduinos have a crippled version of sscanf(). For instance, it does not support the floating point "%f" format specifier. If they removed floating point, then maybe they also removed the field width specifier feature, though I don't know this. I searched but did not find any clear answer.
+ 1
15032208302259-0250000085000000000000000000000033000000000000000000000985913000231747230720G
This is that string, it is one windmill controller's serial data output, I want to slize it and show up in website, for example the first four digits was Generator RPM= 1503,
Next 2 digit was Rotor RPM = 22, next 3 digits was Windspeed = 08.5 m/s like this ,
Thank you 🙏
0
Thank you dude , but is this function available in embedded c? because I need this code to program the Arduino. I'll check and update the result 🤝