# How to use length of a string for collision detection

**URL:** <https://discourse.processing.org/t/how-to-use-length-of-a-string-for-collision-detection/20218>\
**Category:** Coding Questions\
**Created:** [April 28, 2020, 2:31am UTC](https://discourse.processing.org/t/how-to-use-length-of-a-string-for-collision-detection/20218 "2020-04-28T02:31:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [April 28, 2020, 2:31am UTC](https://discourse.processing.org/t/how-to-use-length-of-a-string-for-collision-detection/20218/1 "2020-04-28T02:31:36Z")

</div>

So I have this code that displays a string, and whenever the mouse hovers over it, then it changes colour.

```auto
if (mouseX > 40 && mouseX < text.length() * 17)
    fill(0);

```

The text size is 25. However, it seems that `text.length() * 17` only works for some strings. On shorter strings, you have to move your mouse closer, and on longer strings, you have to move your mouse further.

So is there a way that I can use a string’s length to set boundaries to the string? Thanks

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 28, 2020, 2:37am UTC](https://discourse.processing.org/t/how-to-use-length-of-a-string-for-collision-detection/20218/2 "2020-04-28T02:37:44Z")

</div>

When I have a question like this, I usually start by reading through the reference:

[https://processing.org/reference/](https://processing.org/reference/)

Maybe look for a function that would return the width of your text?

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [April 28, 2020, 6:08pm UTC](https://discourse.processing.org/t/how-to-use-length-of-a-string-for-collision-detection/20218/3 "2020-04-28T18:08:42Z")

</div>

Thanks! I will do that in the future
