In one of my task i need to strip off zero from a string. In the following variable declaration :
var i = A010; (Alphanumeric format type which has 10 spaces)
i need to strip off zero between A and 1. i should not remove the zero which comes after 1.
By using IndexOf i can get the first occurance of zero from this.
Here if i use IndexOf it would be return me a value of 1. Whether i can check what is the value of this 1st position. If the 1st position value is zero i need to strip off it from the string.
How can i do it? Could you please help me out.
I know i can't get the actual code here. But i need a guidance like any other stuff is there like to get the value of a string in a position and also what can i do for a strip off a value from a string?
Thank you very much for the earliest reply on this post. Here not always i would have a variable format type like A010. It would be I9 or D06 or F05.10.
If i always have A010 then i can use replacefirst method.
Since i can substitue A10 with that.
I just need to strip off the first occurance of zero in a string that's it. i don't know what needs to be done for a stipping off zero. whether i can give like "" in the place of zero.
Thank you very much for the earliest reply on this post. Here not always i would have a variable format type like A010. It would be I9 or D06 or F05.10.
If i always have A010 then i can use replacefirst method.
Since i can substitue A10 with that.
I just need to strip off the first occurance of zero in a string that's it. i don't know what needs to be done for a stipping off zero. whether i can give like "" in the place of zero.
I don't see why replaceFirst wouldn't work for this. Have it find "0" and replace it with nothing. Have you tried doing what you suspect will work?
}
public String removeLeadingZeros(String str)
{
if (str == null)
{
return null;
}
char[] chars = str.toCharArray();
int index = 0;
for (; index < str.length(); index++)
{
if (chars[index] != '0')
{
break;
}
}
return (index == 0) ? str : str.substring(index);
}
Hi,
I managed to strip off the leading zeroes and the first occurance using replaceFirst.
for example if the string is 00A10 or A010 it would strip off 00 before A and 0 between A and 1. But what happens if i have 2 or 3 zeroes in between A and 1.
Whether i can use chararray, chck for the indexes having zero and strip off?
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.