Hi,

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?

Thanks in adv
am

The String class has a replaceFirst() method, look into it.

Hi Jas,

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.

Hi Jas,

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?

Hi Jas and Vernon thanks a lot. I used replaceFirst and got the result.

I was thinking of using indexOf and then checking the value is "0" or not and then replacing. But this is straight.

public String getusageFormat()
    {
     removeLeadingZeros(usageFormat);
      String temps1;
        temps1 = usageFormat.replaceFirst("0","");
        return temps1;

    }
    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?

replaceFirst uses a regex, so you could form a regex such as 0+ which means one or more occurrences of zeroes and then have them replaced by "".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.