# Using substring. What am I doing wrong?

**URL:** <https://discourse.processing.org/t/using-substring-what-am-i-doing-wrong/33503>\
**Category:** Beginners\
**Created:** [November 14, 2021, 1:57pm UTC](https://discourse.processing.org/t/using-substring-what-am-i-doing-wrong/33503 "2021-11-14T13:57:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [November 14, 2021, 1:57pm UTC](https://discourse.processing.org/t/using-substring-what-am-i-doing-wrong/33503/1 "2021-11-14T13:57:25Z")

</div>

```auto
println(p.state.substring(0,1), p.state);
if (p.state.substring(0,1) == "W") {
  println("this is a test");
}

```

p.state is “WPawn”  
p.state.substring(0, 1) is “W”

However, “this is a test” is not being printed and I have no clue why.  
All I want to do is get the first character of the p.state string and see if it is “W” or not.

I have also tried the character W but it doesn’t work either

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [November 14, 2021, 2:45pm UTC](https://discourse.processing.org/t/using-substring-what-am-i-doing-wrong/33503/2 "2021-11-14T14:45:37Z")

</div>

```auto
String myString = "Lollipops and Candyfloss";
println(myString.substring(0,1), myString);
if (myString.substring(0,1).equals("L")) {
  println("this is a test");
}

```

reference vs value testing

> **[Java String equals() method - javatpoint](https://www.javatpoint.com/java-string-equals)**
>
> Java String equals and equalsIgnoreCase methods with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string equals in java etc.

you could also do

```auto
if (myString.substring(0,1).charAt(0) == 'L') {
  println("this is a test");
}

```

but i don’t know why you would. instead it would be better to skip the substring altogether if you are just testing a single character and use charAt instead or basically this instead of the first code in this post

```auto
String myString = "Lollipops and Candyfloss";
if (myString.charAt(0) == 'L') {
  println("this is a test");
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 14, 2021, 3:02pm UTC](https://discourse.processing.org/t/using-substring-what-am-i-doing-wrong/33503/3 "2021-11-14T15:02:55Z")

</div>

Hello,

Some Processing references:

- [https://processing.org/](https://processing.org/) Lots of resources here!
- [https://processing.org/reference/equality.html](https://processing.org/reference/equality.html)
- [Strings and Drawing Text / Processing.org](https://processing.org/tutorials/text)

`:)`
