LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

actively remove unwanted characters

Solved!
Go to solution

Hi, I'm writing a function to actively remove unwanted characters from an input field. It will run every time a key press event occurs on the selected input fields. It is going to remove quotes and things like that. This is what I have so far..

 

void test(){
	char unwantedCharSet[50]={"abcD"};
	char tempString2[200];
	int matchedCharIndex;
	GetCtrlVal (panelHandle[MAIN], MAIN_STRING,tempString);
	do{
		RemoveSurroundingWhiteSpace (tempString);
		matchedCharIndex = strcspn (tempString, unwantedCharSet);
		if(matchedCharIndex==strlen(tempString)){
			SetCtrlVal (panelHandle[MAIN], MAIN_STRING, tempString);
			return;
		}
		else{
			strncpy (tempString2, tempString, matchedCharIndex);
			CopyString (tempString, matchedCharIndex, tempString,matchedCharIndex+1,(strlen(tempString)-matchedCharIndex));
			strcat (tempString2, tempString);
		}
		sprintf(tempString,"%s",tempString2);
	} while(1);
return;
}

 

It is still glitchy. Does anyone have any advice to efficiently do this?

 

Thanks in advance!

0 Kudos
Message 1 of 4
(3,354 Views)

Hi,

 

did you consider using regular expressions, via RegExpr_FindPatternInText ?

0 Kudos
Message 2 of 4
(3,349 Views)

Thanks, thats a good one to remember. (I'm still very much in a learning phase)

 

I'm also trying to use what Al S suggested here.

 

void test(){
	char otherString[50];
	GetCtrlVal(panelHandle[MAIN],MAIN_STRING,tempString);
	filter(tempString,otherString);
	SetCtrlVal(panelHandle[MAIN],MAIN_STRING,otherString);
return;
}
int filter(char *inputString, char *outputString){
	int inputPtr, outputPtr = 0;
	for (inputPtr=0;inputPtr<strlen(inputString);inputPtr++,outputPtr++){
		if (inputString[inputPtr]==41||inputString[inputPtr]==30){
			inputPtr++;  // increment the inputString pointer to skip the backspace
			outputPtr--; // decrement the outputString pointer to overwrite
  		}
  		outputString[outputPtr]=inputString[inputPtr];
 	}
	outputString[outputPtr]='\0';
return 0;
}

 

0 Kudos
Message 3 of 4
(3,346 Views)
Solution
Accepted by topic author TurboMetrologist

This was my solution, although, it takes a long time to perform. Does anyone have any tips on a better (faster/cleaner) approach?

 

/*=====================================================================*/
//  TEST
/*=====================================================================*/
void test(){
	GetCtrlVal(panelHandle[MAIN],MAIN_STRING,tempString);
	SetCtrlVal(panelHandle[MAIN],MAIN_STRING,filter(tempString,unwantedCharSet));
return;
}
/*=====================================================================*/
//  FILTER
/*=====================================================================*/
char *filter(char *inputString,char *filterString){
	int ptr=0,matchedCharIndex=0;
	char outputString[100];
	do{	
		matchedCharIndex = strcspn (inputString,filterString);
		if(matchedCharIndex!=strlen(tempString)){
			strcpy (outputString, inputString);
			for(ptr=matchedCharIndex;ptr<=strlen(inputString);ptr++){
				outputString[ptr]=inputString[ptr+1];
				DebugPrintf("%s\n",outputString);
				if(outputString[ptr]=='\n'||outputString[ptr]=='\0')
					break;
			}
			strcpy(inputString,outputString);
		}
		else
			return inputString;
	} while(1);
return inputString;
}

 

 

 

0 Kudos
Message 4 of 4
(3,323 Views)