943,531 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 970
  • Java RSS
Mar 15th, 2009
0

Need to strip off zero from a string

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
av11453 is offline Offline
7 posts
since Oct 2008
Mar 15th, 2009
0

Re: Need to strip off zero from a string

The String class has a replaceFirst() method, look into it.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 15th, 2009
0

Re: Need to strip off zero from a string

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
av11453 is offline Offline
7 posts
since Oct 2008
Mar 16th, 2009
0

Re: Need to strip off zero from a string

Click to Expand / Collapse  Quote originally posted by av11453 ...
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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Mar 18th, 2009
0

Re: Need to strip off zero from a string

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
av11453 is offline Offline
7 posts
since Oct 2008
Mar 19th, 2009
0

Re: Need to strip off zero from a string

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
av11453 is offline Offline
7 posts
since Oct 2008
Mar 19th, 2009
0

Re: Need to strip off zero from a string

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

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.
Message:
Previous Thread in Java Forum Timeline: Redirecting System.out
Next Thread in Java Forum Timeline: Little help with RandomAccessFile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC