# Unable to shorten strings

**URL:** <https://discourse.processing.org/t/unable-to-shorten-strings/4046>\
**Category:** Coding Questions\
**Created:** [October 2, 2018, 2:11pm UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046 "2018-10-02T14:11:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![desk](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@desk](https://discourse.processing.org/u/desk)\
**Post date:** [October 2, 2018, 2:11pm UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/1 "2018-10-02T14:11:05Z")

</div>

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

```auto
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);
  }  
}

```

 ![Screenshot%20from%202018-10-02%2022-10-13](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/191dde8dc622014360f5a6919f0d762d1a954715.png)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 2, 2018, 4:25pm UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/2 "2018-10-02T16:25:40Z")

</div>

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

```java
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: 🙃

```javascript
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 += " ";
}

```

---

<div class="post-metadata">

**Author:** ![desk](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@desk](https://discourse.processing.org/u/desk)\
**Post date:** [October 3, 2018, 12:11am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/3 "2018-10-03T00:11:28Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 3, 2018, 2:02am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/4 "2018-10-03T02:02:00Z")

</div>

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] ?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 2:36am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/5 "2018-10-03T02:36:02Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![desk](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@desk](https://discourse.processing.org/u/desk)\
**Post date:** [October 3, 2018, 4:27am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/6 "2018-10-03T04:27:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 4:30am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/7 "2018-10-03T04:30:31Z")

</div>

> [@desk](#):
>
> … not sure if it’s real or just perceived…

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

---

<div class="post-metadata">

**Author:** ![desk](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@desk](https://discourse.processing.org/u/desk)\
**Post date:** [October 3, 2018, 5:10am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/8 "2018-10-03T05:10:49Z")

</div>

[GoToLoop](https://discourse.processing.org/u/GoToLoop), You’re right. It’s just perceived. Small correction though. The bob.length would need to be at the beginning.

```auto
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](https://discourse.processing.org/u/kll), I’ve moved the code to keyTyped which helps.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 3, 2018, 5:33am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/9 "2018-10-03T05:33:28Z")

</div>

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()

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

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

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 6:18am UTC](https://discourse.processing.org/t/unable-to-shorten-strings/4046/10 "2018-10-03T06:18:08Z")

</div>

> [@desk](#):
>
> The _bob.length_ would need to be at the beginning.

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

> [@desk](#):
>
> 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.

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

> **[String.prototype.substring() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring)**
>
> The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

However, Java’s String::**substring()** will `throw` [IndexOutOfBoundsException](https://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/IndexOutOfBoundsException.html) for any invalid arguments: 🐛

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