Need to strip off zero from a string

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 7
Reputation: av11453 is an unknown quantity at this point 
Solved Threads: 0
av11453 av11453 is offline Offline
Newbie Poster

Need to strip off zero from a string

 
0
  #1
Mar 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Need to strip off zero from a string

 
0
  #2
Mar 15th, 2009
The String class has a replaceFirst() method, look into it.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: av11453 is an unknown quantity at this point 
Solved Threads: 0
av11453 av11453 is offline Offline
Newbie Poster

Re: Need to strip off zero from a string

 
0
  #3
Mar 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need to strip off zero from a string

 
0
  #4
Mar 16th, 2009
Originally Posted by av11453 View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: av11453 is an unknown quantity at this point 
Solved Threads: 0
av11453 av11453 is offline Offline
Newbie Poster

Re: Need to strip off zero from a string

 
0
  #5
Mar 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: av11453 is an unknown quantity at this point 
Solved Threads: 0
av11453 av11453 is offline Offline
Newbie Poster

Re: Need to strip off zero from a string

 
0
  #6
Mar 19th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Need to strip off zero from a string

 
0
  #7
Mar 19th, 2009
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 "".
Last edited by verruckt24; Mar 19th, 2009 at 11:52 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC