https://github.com/geeksloth/get-value-by-separator
Extracts a substring from a given string based on a separator and an index. Works on native C language and Arduino.
https://github.com/geeksloth/get-value-by-separator
arduino
Last synced: 26 days ago
JSON representation
Extracts a substring from a given string based on a separator and an index. Works on native C language and Arduino.
- Host: GitHub
- URL: https://github.com/geeksloth/get-value-by-separator
- Owner: geeksloth
- Created: 2024-11-21T14:22:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-21T14:52:16.000Z (over 1 year ago)
- Last Synced: 2025-03-16T06:43:12.087Z (over 1 year ago)
- Topics: arduino
- Language: C++
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# get-value-by-separator
Extracts a substring from a given string based on a separator and an index.
Works on native C language and Arduino.
# Usage
```cpp
String text = "apple,banana,cherry";
String result = getValue(text, ',', 1);
// result will be "banana"
```
In this example, the function will return "banana" as it is the substring at index 1 when the string is split by the comma separator.
# Code
This function searches for the nth occurrence of a separator character in the input string and returns the substring located at that position.
```cpp
/**
* @brief Extracts a substring from a given string based on a separator and an index.
*
* This function searches for the nth occurrence of a separator character in the input string
* and returns the substring located at that position.
*
* @param text The input string from which the substring will be extracted.
* @param separator The character used to separate the substrings within the input string.
* @param index The zero-based index of the substring to be extracted.
* @return A substring located at the specified index, or an empty string if the index is out of bounds.
*/
String getValue(String text, char separator, int index) {
int found = 0;
int string_index[] = {0, -1};
int max_index = text.length()-1;
for (int i = 0; i <= max_index && found <= index; i++) {
if (text.charAt(i) == separator || i == max_index){
found++;
string_index[0] = string_index[1] + 1;
string_index[1] = (i == max_index) ? i + 1 : i;
}
}
return found > index ? text.substring(string_index[0], string_index[1]) : "";
}
```