Help to condense coding that deletes unwanted characters in textField

Everything works fine, but I have to do the “for w, do” multiple times for different symbols (maybe a total of 10 symbols or so). As I’m pretty sure that these are globals and slow down my application, is there a way to add an if/then statement saying if the input is not a number or a + or - sign then delete the entry?

second question  

     With the below method (when testing on device), when it deletes letters it also moves the position of the flashing vertical bar (that tells you where your next character will appear). So that if they typed 4a, the a will delete and the flashing bar will appear before the 4. Any way to make the flashing bar shift over?

[lua]

if ( “editing” == event.phase ) then

for w in string.gmatch(input1.text, “%a”) do

input1.text = string.gsub(input1.text, “%a”, “”)

print (“getting rid of letters”)

–native.setKeyboardFocus( input1 )

end

for w in string.gmatch(input1.text, “%s”) do

input1.text = string.gsub(input1.text, “%s”, “”)

print (“getting rid of space”)

end

end

[/lua]

Here’s what I’m playing around with. I wanted it to only print the statement when input is not equal to a number, but it still prints the statement for both numbers and letters.

[lua] if input1.text ~= string.match( input1.text, “(%d)” then

print (“here”)

end

[/lua]

Hi @Nate112,

For clarity in this topic, can you please post a few examples of what a string might look like before “processing” it, and what you want it to look like afterward? This should help readers see exactly what kind of string manipulation you’re working toward.

Thanks,

Brent

It’s basically an addition calculator. So only numbers, decimals and addition signs allowed. One of the problem is that on certain devices, the numbers keyboard doesn’t include the plus sign. So I’m allowing the alpha numeric keyboard which includes all symbols

Hi @Nate112,

In this case, I would opt for a “gsub()” where you detect all letters, along with every possible “symbol” that you want to disallow from the alphanumeric keypad, and replace them all with an empty string. It can be done in one swoop if you write the matching pattern correctly. For example:

[lua]

local before = “123abc,456 + 991”

local after = string.gsub( before, “([%a%s%,]+)”, “” )

print( after )

[/lua]

Notice how I detect all letters with " %a" and all spaces with " %s". Then I check comma with " %," (prefixing with % is necessary since you want to match on that specific character). You’d also need to include every other disallowed special symbol from the alphanumeric keyboard, separated by " ****" in the pattern. I realize this could be a lot of special cases to check for, but it should be far more efficient than a bunch of looping iterations.

Take care,

Brent

Okay thanks for the help, I appreciate it. Btw, is ([%a+%s+%,+]) the same as (%a%s%,]+)?

Also do you know anything about my other question?

 

     “With the below method (when testing on device), when it deletes letters it also moves the position of the flashing vertical bar (that tells you where your next character will appear). So that if they typed 4a, the a will delete and the flashing bar will appear before the 4. Any way to make the flashing bar shift over?”

Hi @Nate112,

Yes, you can position the cursor using the “setSelection()” function:

http://docs.coronalabs.com/api/type/TextField/setSelection.html

Brent

fwiw, might be simpler to invert ^ the “keeper” char set, fe

local before = “123junk + 234 - JUNK345 * 456 / ju567NK = 6j7u8n9k0”

local pattern = “[^%d%+%-%*%/%=]+”

local after = string.gsub( before, pattern, “” )

print(after)

Okay thanks guys, I just tried it out and mostly everything works well. The only exception is when I try to add %"\ in the mix, it reads the " symbol to be the end quotation mark. So for example:

[lua]

  1. local before = “123abc,456 + 991”
  2. local after = string.gsub( before, “([%a%s%,\ %"]+)”, “” )
  3. print( after )

[/lua]

Will make what I highlighted from pink text to black text in text wrangler because it doesn’t recognize me talking about the " symbol within the two surrounding quotation marks. 

Terminal reads: “’)’ expected near ‘]’”

Thanks for the help

Hi @Nate112,

OK, this requires some special handling then. First, it should work if you change the double quotes to single quotes as such:

[lua]

local after = string.gsub( before, ‘([%a%s%,%"]+)’, “” )

[/lua]

However, then you’ll probably face the possibility that they’ll use a single quote (from the keyboard) which you also want to filter out. So, you might need to take the result of the above, then do another .gsub() on it which filters out that character, and this time use double quotes surrounding it. So…

[lua]

local step1 = string.gsub( before, ‘([%a%s%,%"]+)’, “” )

local after = string.gsub( step1, “(%’+)”, “” )

print( after )

[/lua]

Brent

Here’s what I’m playing around with. I wanted it to only print the statement when input is not equal to a number, but it still prints the statement for both numbers and letters.

[lua] if input1.text ~= string.match( input1.text, “(%d)” then

print (“here”)

end

[/lua]

Hi @Nate112,

For clarity in this topic, can you please post a few examples of what a string might look like before “processing” it, and what you want it to look like afterward? This should help readers see exactly what kind of string manipulation you’re working toward.

Thanks,

Brent

It’s basically an addition calculator. So only numbers, decimals and addition signs allowed. One of the problem is that on certain devices, the numbers keyboard doesn’t include the plus sign. So I’m allowing the alpha numeric keyboard which includes all symbols

Hi @Nate112,

In this case, I would opt for a “gsub()” where you detect all letters, along with every possible “symbol” that you want to disallow from the alphanumeric keypad, and replace them all with an empty string. It can be done in one swoop if you write the matching pattern correctly. For example:

[lua]

local before = “123abc,456 + 991”

local after = string.gsub( before, “([%a%s%,]+)”, “” )

print( after )

[/lua]

Notice how I detect all letters with " %a" and all spaces with " %s". Then I check comma with " %," (prefixing with % is necessary since you want to match on that specific character). You’d also need to include every other disallowed special symbol from the alphanumeric keyboard, separated by " ****" in the pattern. I realize this could be a lot of special cases to check for, but it should be far more efficient than a bunch of looping iterations.

Take care,

Brent

Okay thanks for the help, I appreciate it. Btw, is ([%a+%s+%,+]) the same as (%a%s%,]+)?

Also do you know anything about my other question?

 

     “With the below method (when testing on device), when it deletes letters it also moves the position of the flashing vertical bar (that tells you where your next character will appear). So that if they typed 4a, the a will delete and the flashing bar will appear before the 4. Any way to make the flashing bar shift over?”

Hi @Nate112,

Yes, you can position the cursor using the “setSelection()” function:

http://docs.coronalabs.com/api/type/TextField/setSelection.html

Brent

fwiw, might be simpler to invert ^ the “keeper” char set, fe

local before = “123junk + 234 - JUNK345 * 456 / ju567NK = 6j7u8n9k0”

local pattern = “[^%d%+%-%*%/%=]+”

local after = string.gsub( before, pattern, “” )

print(after)

Okay thanks guys, I just tried it out and mostly everything works well. The only exception is when I try to add %"\ in the mix, it reads the " symbol to be the end quotation mark. So for example:

[lua]

  1. local before = “123abc,456 + 991”
  2. local after = string.gsub( before, “([%a%s%,\ %"]+)”, “” )
  3. print( after )

[/lua]

Will make what I highlighted from pink text to black text in text wrangler because it doesn’t recognize me talking about the " symbol within the two surrounding quotation marks. 

Terminal reads: “’)’ expected near ‘]’”

Thanks for the help

Hi @Nate112,

OK, this requires some special handling then. First, it should work if you change the double quotes to single quotes as such:

[lua]

local after = string.gsub( before, ‘([%a%s%,%"]+)’, “” )

[/lua]

However, then you’ll probably face the possibility that they’ll use a single quote (from the keyboard) which you also want to filter out. So, you might need to take the result of the above, then do another .gsub() on it which filters out that character, and this time use double quotes surrounding it. So…

[lua]

local step1 = string.gsub( before, ‘([%a%s%,%"]+)’, “” )

local after = string.gsub( step1, “(%’+)”, “” )

print( after )

[/lua]

Brent