# How to truncate, limit, clamp, constrain, shorten, etc ... a string?

**URL:** <https://discourse.processing.org/t/how-to-truncate-limit-clamp-constrain-shorten-etc-a-string/26458>\
**Category:** Beginners\
**Created:** [December 23, 2020, 3:42am UTC](https://discourse.processing.org/t/how-to-truncate-limit-clamp-constrain-shorten-etc-a-string/26458 "2020-12-23T03:42:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaseyJ](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/caseyj/32/15118_2.png) [@CaseyJ](https://discourse.processing.org/u/CaseyJ)\
**Post date:** [December 23, 2020, 3:42am UTC](https://discourse.processing.org/t/how-to-truncate-limit-clamp-constrain-shorten-etc-a-string/26458/1 "2020-12-23T03:42:30Z")

</div>

I have a better feeling for how to limit the run on numbers in a long decimal using the [nf()](https://processing.org/reference/nf_.html) function in Processing.

However, how do you do this with a string?

I see no alternative so maybe you can still use nf()?

For instance I have a text input field and I want to limit the number of characters that can be listed. Such as only 10 characters, etc.

Thank you!

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 23, 2020, 8:15am UTC](https://discourse.processing.org/t/how-to-truncate-limit-clamp-constrain-shorten-etc-a-string/26458/2 "2020-12-23T08:15:16Z")

</div>

Someting like this?

```auto
import static javax.swing.JOptionPane.*; 

String words = "";

void setup() {
  size(640, 360);
  textSize(20);
}

void draw() {
  background(0);
  text("Type a string. (Only 10 characters allowed!)", 50, 90);
  text(words, 50, 120, 540, 300);
  if (words.length() > 10) { 
    showMessageDialog(frame, "Reached 10 characters limit");
    words = words.substring(0, words.length() - 1);
  }
}

void keyTyped() {
  if ((key >= 'A' && key <= 'z') || key == ' ') {
    words = words + key;
  }
}

```

---

<div class="post-metadata">

**Author:** ![CaseyJ](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/caseyj/32/15118_2.png) [@CaseyJ](https://discourse.processing.org/u/CaseyJ)\
**Post date:** [December 24, 2020, 4:19am UTC](https://discourse.processing.org/t/how-to-truncate-limit-clamp-constrain-shorten-etc-a-string/26458/3 "2020-12-24T04:19:52Z")

</div>

Wow that is amazing nice work! That dialogue box is super awesome too I had no idea that was an option.

I will definitely play around with this and see what I can learn.

Thank you very much!!
