09-22-2010 09:39 AM - edited 09-22-2010 09:42 AM
Hi, experienced CVI guys,
Are there some functions shipped with CVI to replace or delete specific characters in a string?
For example, I want to replace all \/|- with an empty string, or to delete them. What can I do? Loops are too complex.
Original string:
Erasing Device...
\|/- \ \ \\|/-\|/ \
Report
07-Sep-2010, 08:04:55
Result I want is
Erasing Device...
Report
07-Sep-2010, 08:04:55
Thanks.
Solved! Go to Solution.
09-22-2010 03:47 PM - edited 09-22-2010 03:49 PM
labc:
How generic do you want to make your filter?
For our discussion, I'll call your series of characters a text spinner. I'm assuming the number of characters in it may vary, but that the pattern is consistent in the respect: the spinner is made up of characters like \ / | -, each followed by the ASCII backspace (char) 8. (That's 8 cast as a character, which is not the same as '8'.) In your sample spinner pattern, every occurrence of the ASCII backspace is preceded by one of the spinner characters. Let's assume that this pattern is constant.
So what I'd do is search through your input string (yes, using a loop) and copy the input string byte by byte to an output string. If you find an ASCII backspace in the input string, skip it, and overwrite the last character in the output string (which is one of the spinner characters) with the next character in the input string.
Yes, you are using a loop, but it's not too complex.
Here's the whole loop, written as a function.
// filterSpinner function to strip the text spinner characters out of a string.
// The text spinner is made up of \ / | - characters separated by (char) 8 (ASCII backspace)
int filterSpinner(char *inputString, char *outputString)
{
int inputPtr, outputPtr = 0;
// walk though the input string
for (inputPtr = 0; inputPtr < strlen(inputString); inputPtr++, outputPtr++)
{
// search for the ASCII backspace character that's part of the spinner
if (inputString[inputPtr] == 😎
{
// if the ASCII backspace is found,
inputPtr++; // increment the inputString pointer to skip the backspace
outputPtr--; // decrement the outputString pointer to overwrite
// the spinner character that preceded the backspace
}
outputString[outputPtr] = inputString[inputPtr];
}
outputString[outputPtr] = '\0';
return 0;
}
You'll also find an attached CVI program which uses it on your sample string.
09-22-2010 06:35 PM
Hi, AI S,
Thanks for your analysis and code. That's what I'd like.
My direction was not wrong, but I used an worse efficient filter. So I thought it's complex.