Remove String function


Recommended Posts

I am trying to filter out all characters except A-Z, a-z, 0-9.The resultant string is used for a filename.

I have had some success with remove(String,Remove Char) but this is tedious because every unwanted char has be be enumerated in a separate statement, as in the attached .ctl file.

Is there a simpler way to do this with a different method or range of values?.

Also, for some reason the line removing the "@' symbol does not seem to work.

Please see the attached DF file which shows the method that I am using.

 

text conversion.ctl

Link to comment
Share on other sites

The @ doesn't work because it is used for foreign language support.  To actually create a string that contains "@", you need to do two: "@@"

The best way to do this is probably to use asca() to convert to an array, use filter() to remove the desired chars, then chra() to convert it back.  Because the ASCII codes are in sequence, the expression isn't too bad:

private x = asca(originalstring)

private string filtered = chra(filter(x, (x >= asc("0") && x <= asc("9")) || (x >= asc("A") && x <= asc("Z")) || (x >= asc("a") && x <= asc("z"))))

 

You could replace all the asc("0") with the actual character code, but my form is more readable.

 

Link to comment
Share on other sites

On the same subject, I have the following code,that filters out everything except numeric input. This method works, except that there are seven more sets of 3 just like this, and writing a separate filter for each one seems like overkill. Can this be done with a single filter using variables? If so, please provide an example. Thanks.

 

Private a =AscA(newSpeedSP)
Private b =AscA(newTorqueSP)
Private c =AscA(newTimeSP)
private string nSF
private string nTrqF
private string nTmeF
private string newrecipe

private string nSF = chra(filter(a, (a >= asc("0") && a <= asc("9"))))

private string nTrqF = chra(filter(b, (b >= asc("0") && b <= asc("9"))))

private string nTmeF = chra(filter(c, (c >= asc("0") && c <= asc("9"))))

newrecipe[0][fName] = NewRecipeName
switch
   case (NewSpeedSP>0)
      newrecipe[0][fSpeedSP] = nSF
   default
      newrecipe[0][fSpeedSP] =0
endcase
switch  
case (NewTorqueSP>0)
      newrecipe[0][fTorqueSP] = nTrqF
   default
      newrecipe[0][fTorqueSP] =0
endcase
switch 
 case  (NewTimeSP>0)
      newrecipe[0][fTimeSP] = nTmeF
  default
      newrecipe[0][fTimeSP] =0
endcase
 

Link to comment
Share on other sites

Thank you. That does work,, as in the attached test file.However,. note that the test file has two different input boxes and has an output box that is associated with each input box.

Can you please explain how to direct the function output to the appropriate box that is associated with each input?

Thanks

text conversion_2.ctl

Link to comment
Share on other sites

Sorry, I did not look at the file the first time, but simply replied to the question.

First, you really don't want to use edit boxes on main pages.  See my blog post concerning this.  Use a variable value component with the Set To action, or a popup window instead.  The variable value component is easier.

Second, you don't want to create a global called originalstring, since you use it as a local int he stripString function.  Plus you need two globals for the two values.  So, call them input1 and input2 or something, declared in Startup.  

Finally, stripString() takes a parameter, so you need to pass something to it.  So, your expression in the Filtered Text display should be:

stripstring(input1)

and 

stripstring(input2)

assuming you use those variable names.

Link to comment
Share on other sites

Thank you, that works perfectly.

As to your blog post on input boxes, I agree with the principles of that post.

However, in this case, these input boxes are not on the main page but serve as inputs to a recipe. The recipe is 8 stages of speed, torque and time for testing gearboxes. This recipe is then loaded into an array and becomes the test profile.

The addrecipe Sequence is now::

	private string newrecipe
	newrecipe[0][fName] = NewRecipeName
switch
   case (NewSpeedSP>0)
      newrecipe[0][fSpeedSP] = StripString(NewSpeedSP)
   default
      newrecipe[0][fSpeedSP] =0
endcase
switch  
case (NewTorqueSP>0)
      newrecipe[0][fTorqueSP] = StripString(NewTorqueSP)
   default
      newrecipe[0][fTorqueSP] =0
endcase
switch 
 case  (NewTimeSP>0)
      newrecipe[0][fTimeSP] = StripString(NewTimeSP)
  default
      newrecipe[0][fTimeSP] =0
endcase
switch
case (NewSpeedSP1>0)
      newrecipe[0][fSpeedSP1] =  StripString(NewSpeedSP1)
  default
   newrecipe[0][fSpeedSP1] =0
endcase
switch
case (NewTorqueSP1>0)
      newrecipe[0][fTorqueSP1] = StripString(NewTorqueSP1)
  default
      newrecipe[0][fTorqueSP1] =0
endcase
switch
case (NewTimeSP1>0)
      newrecipe[0][fTimeSP1] = StripString(NewTimeSP1)
  default
      newrecipe[0][fTimeSP1] =0
endcase
switch
case (NewSpeedSP2>0)
      newrecipe[0][fSpeedSP2] =  StripString(NewSpeedSP2)
  default
      newrecipe[0][fSpeedSP2] =0
endcase
switch
case (NewTorqueSP2>0)
      newrecipe[0][fTorqueSP2] = StripString(NewTorqueSP2)
  default
      newrecipe[0][fTorqueSP2] =0
endcase
switch
case (NewTimeSP2>0)
      newrecipe[0][fTimeSP2] = StripString(NewTimeSP2)
  default
      newrecipe[0][fTimeSP2] =0
endcase
switch
case (NewSpeedSP3>0)
      newrecipe[0][fSpeedSP3] =  StripString(NewSpeedSP3)
  default
      newrecipe[0][fSpeedSP3] =0
endcase
switch
case (NewTorqueSP3>0)
      newrecipe[0][fTorqueSP3] = StripString(NewTorqueSP3)
  default
      newrecipe[0][fTorqueSP3] =0
endcase
switch
case (NewTimeSP3>0)
      newrecipe[0][fTimeSP3] = StripString(NewTimeSP3)
  default
      newrecipe[0][fTimeSP3] =0
endcase
switch
case (NewSpeedSP4>0)
      newrecipe[0][fSpeedSP4] =  StripString(NewSpeedSP4)
  default
      newrecipe[0][fSpeedSP4] =0
endcase
switch
case (NewTorqueSP4>0)
      newrecipe[0][fTorqueSP4] = StripString(NewTorqueSP4)
  default
      newrecipe[0][fTorqueSP4] =0
endcase
switch
case (NewTimeSP4>0)
      newrecipe[0][fTimeSP4] = StripString(NewTimeSP4)
  default
      newrecipe[0][fTimeSP4] =0
endcase
switch
case (NewSpeedSP5>0)
      newrecipe[0][fSpeedSP5] =  StripString(NewSpeedSP5)
  default
      newrecipe[0][fSpeedSP5] =0
endcase
switch
case (NewTorqueSP5>0)
      newrecipe[0][fTorqueSP5] = StripString(NewTorqueSP5)
  default
      newrecipe[0][fTorqueSP5] =0
endcase
switch
case (NewTimeSP5>0)
      newrecipe[0][fTimeSP5] = StripString(NewTimeSP5)
  default
      newrecipe[0][fTimeSP5] =0
endcase
switch
case (NewSpeedSP6>0)
      newrecipe[0][fSpeedSP6] =  StripString(NewSpeedSP6)
  default
      newrecipe[0][fSpeedSP6] =0
endcase
switch
case (NewTorqueSP6>0)
      newrecipe[0][fTorqueSP6] = StripString(NewTorqueSP6)
  default
      newrecipe[0][fTorqueSP6] =0
endcase
switch
case (NewTimeSP6>0)
      newrecipe[0][fTimeSP6] = StripString(NewTimeSP6)
  default
      newrecipe[0][fTimeSP6] =0
endcase
switch
case (NewSpeedSP7>0)
      newrecipe[0][fSpeedSP7] =  StripString(NewSpeedSP7)
  default
      newrecipe[0][fSpeedSP7] =0
endcase
switch
case (NewTorqueSP7>0)
      newrecipe[0][fTorqueSP7] = StripString(NewTorqueSP7)
  default
      newrecipe[0][fTorqueSP7] =0
endcase
switch
case (NewTimeSP7>0)
      newrecipe[0][fTimeSP7] = StripString(NewTimeSP7)
  default
      newrecipe[0][fTimeSP7] =0
endcase
	recipes.Append(newrecipe)
	ResetDisplay()
	

 

And in this instance, I only wanted numeric data input to the array, so the StripString Function is thus:

function stripString(string originalstring)
   private x = asca(originalstring)
return (chra(filter(x, (x >= asc("0") && x <= asc("9")))))

Thanks for the assist.

Link to comment
Share on other sites

On a related subject regarding edit text boxes, is it possible to set its properties so that when the edit box is clicked, the existing data in the box is highlighted so that when new input is added it will overwrite the existing data in box?

 

i.e., the box has data "Enter Name".

When user Mouse clicks on data box, jt highlights that text so that when name is entered, that old data is erased.

Link to comment
Share on other sites

Also, because I am using getFocus & setFocus, entering data is much faster than a whole bunch of system popup boxes

 

OnKeyDown Sequence:

	
if (key==13)
 switch
case  (component.Name_EditBox.GetFocus())
         component.SpeedSP1_EditBox.SetFocus()
case  (component.SpeedSP1_EditBox.GetFocus())
         component.TorqueSP1_EditBox.SetFocus()
case  (component.TorqueSP1_EditBox.GetFocus())
         component.TimeSP1_EditBox.SetFocus()  
case  (component.TimeSP1_EditBox.GetFocus())
         component.SpeedSP2_EditBox.SetFocus()
case  (component.SpeedSP2_EditBox.GetFocus())
         component.TorqueSP2_EditBox.SetFocus()
case  (component.TorqueSP2_EditBox.GetFocus())
         component.TimeSP2_EditBox.SetFocus()
case  (component.TimeSP2_EditBox.GetFocus())
         component.SpeedSP3_EditBox.SetFocus()
case  (component.SpeedSP3_EditBox.GetFocus())
         component.TorqueSP3_EditBox.SetFocus()
case  (component.TorqueSP3_EditBox.GetFocus())
         component.TimeSP3_EditBox.SetFocus()
case  (component.TimeSP3_EditBox.GetFocus())
         component.SpeedSP4_EditBox.SetFocus()
case  (component.SpeedSP4_EditBox.GetFocus())
         component.TorqueSP4_EditBox.SetFocus()
case  (component.TorqueSP4_EditBox.GetFocus())
         component.TimeSP4_EditBox.SetFocus()
case  (component.TimeSP4_EditBox.GetFocus())
         component.SpeedSP5_EditBox.SetFocus()
case  (component.SpeedSP5_EditBox.GetFocus())
         component.TorqueSP5_EditBox.SetFocus()         
case  (component.TorqueSP5_EditBox.GetFocus())
         component.TimeSP5_EditBox.SetFocus()       
case  (component.TimeSP5_EditBox.GetFocus())
         component.SpeedSP6_EditBox.SetFocus()
case  (component.SpeedSP6_EditBox.GetFocus())
         component.TorqueSP6_EditBox.SetFocus()
case  (component.TorqueSP6_EditBox.GetFocus())
         component.TimeSP6_EditBox.SetFocus()
case  (component.TimeSP6_EditBox.GetFocus())
         component.SpeedSP7_EditBox.SetFocus()
case  (component.SpeedSP7_EditBox.GetFocus())
         component.TorqueSP7_EditBox.SetFocus()
case  (component.TorqueSP7_EditBox.GetFocus())
         component.TimeSP7_EditBox.SetFocus()
case  (component.TimeSP7_EditBox.GetFocus())
         component.SpeedSP8_EditBox.SetFocus()
case  (component.SpeedSP8_EditBox.GetFocus())
         component.TorqueSP8_EditBox.SetFocus()        
case  (component.TorqueSP8_EditBox.GetFocus())
         component.TimeSP8_EditBox.SetFocus()
case  (component.TimeSP8_EditBox.GetFocus())
         component.Name_EditBox.SetFocus()
case  (component.Cust_Name.GetFocus())
         component.Job_No.SetFocus()
case  (component.Job_No.GetFocus())
         component.Cust_Name.SetFocus()
case  (component.Cust_Name1.GetFocus())
         component.Job_No1.SetFocus()
case  (component.Job_No1.GetFocus())
         component.Cust_Name1.SetFocus()
         
 endcase        
 endif
	

Link to comment
Share on other sites

  • 2 months later...

Archived

This topic is now archived and is closed to further replies.