0

I have an array of String, and the items are like : 02:11, 11:12..
I want to delete the 0 in first position.

if (orari[i].substring(0,0) == "0") {
   orari[i] = orari[i].substring(1,4);
}

It doesn't work, why?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Corra
  • 5
  • 1

3 Answers3

2

Firstly your substring bounds are faulty. 0, 0 means 0 length string. That will not get you first character. You should use 0, 1. And then use equals() method for String comparison:

if (orari[i].substring(0,1).equals("0"))

Also, you can avoid that substring in if condition by using charAt() method:

if (orari[i].charAt(0) == '0')
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Because it should be like this:

if (orari[i].charAt(0) == '0') {
   orari[i] = orari[i].substring(1,4);
}
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0
if (orari[i].substring(0,0).equals("0")) {
   orari[i] = orari[i].substring(1,4);
}

use equals() to compare string, not ==

Eric
  • 22,183
  • 20
  • 145
  • 196