Unable to shorten strings

This seems like a simple problem… but it has been driving me nuts!

var bob = '';

function setup() {
}
function draw() {
}

function keyTyped()
{
    bob += key;
    print(bob);
}
function keyPressed()
{
  if(keyCode == ENTER||keyCode == RETURN)
  {
    bob = '';
  }
  else if(keyCode == DELETE||keyCode == BACKSPACE) 
  {
    if(bob.length>0)
      bob = bob.substring(0,bob.length-1);
    print(bob.length);
  }  
}

1 Like

From this Java sketch link below: :coffee:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR

void keyPressed() {
  if (key != CODED | idx < 0)  return;
  final int k = keyCode;
 
  final TextBox tbox = tboxes[idx];
  final int len = tbox.txt.length();
 
  if (k == LEFT)  tbox.txt = tbox.txt.substring(0, max(0, len-1));
  else if (k == RIGHT & len < tbox.lim-3)  tbox.txt += "    ";
}

Which in JS would be something like this: :upside_down_face:

function keyPressed() {
  if (idx < 0)  return;
  const k = keyCode;
 
  const tbox = tboxes[idx];
  const len = tbox.txt.length;
 
  if (k == LEFT)  tbox.txt = tbox.txt.substring(0, max(0, len-1));
  else if (k == RIGHT & len < tbox.lim-3)  tbox.txt += "    ";
}
2 Likes

Thanks GTL, I got this idea after reading your suggestion.

function keyPressed()
{
  if(keyCode == ENTER||keyCode == RETURN)
    bob = '';
  else if(keyCode == DELETE||keyCode == BACKSPACE) 
  {
    if(bob.length>0)
      bob = bob.substring(0,bob.length-1);
  }
  else
    bob += key;
}
1 Like

yes, now with the if else structure
you not add the “Delete” key to the string ( before you do the delete ),
still what happens with all other keys what are not characters, like [SHIFT][x] ?

1 Like

You can also move in the bob.length > 0 condition expression to the main 1: :sunglasses:

else if (keyCode == DELETE | keyCode == BACKSPACE && bob.length)
  bob = bob.substring(0, bob.length - 1);
2 Likes

Thanks for the tips; I do plan to refactor later. I’m just in a mad dash to get the code running. I do like to reduce the number of lines… but I’ve found, in my limited javascript experience, that engines like extra brackets… not sure if it’s real or just perceived

The excerpt I’ve posted is 100% valid JS syntax! :money_mouth_face:

1 Like

GoToLoop, You’re right. It’s just perceived. Small correction though. The bob.length would need to be at the beginning.

else if (bob.length && keyCode == DELETE | keyCode == BACKSPACE )
  bob = bob.substring(0, bob.length - 1);

But it turns out that I don’t even need to test for the length because substring will accept -1 in the second argument without causing any issues I’ve seen.

kll, I’ve moved the code to keyTyped which helps.

function keyTyped()
{
  if(keyCode == ENTER||keyCode == RETURN) 
    bob = '';
  else if(keyCode == DELETE||keyCode == BACKSPACE)
     bob = bob.substring(0,bob.length-1);
  else
    bob += key;
  print(bob);
}
1 Like

keyTyped does not know keyCode but you can use it for
bob+=key;
AND [SHIFT][char] seems to work!
( but seems it adds DELETE key anyhow??)

and i would suggest a && ( b || c ) for the logic in keyPresssed()

else if (bob.length && (keyCode === DELETE || keyCode === BACKSPACE)) {    
 bob = bob.substring(0,bob.length-1); 
}

 print("bob "+bob+" length "+bob.length);
1 Like

Have you actually checked that out? AFAIK, both ways should result the same. :motorway:

Nice catch! JS’ String::substring() simply ignores invalid passed arguments to it: :crazy_face:

However, Java’s String::substring() will throw IndexOutOfBoundsException for any invalid arguments: :bug:

Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#substring(int,int)

2 Likes