سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Because it should be like this:

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

use equals() to compare string, not ==

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top