| | |
Need to strip off zero from a string
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
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.
replaceFirst wouldn't work for this. Have it find "0" and replace it with nothing. Have you tried doing what you suspect will work? •
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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?
{
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 "". 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 !!!
![]() |
Other Threads in the Java Forum
- Previous Thread: Redirecting System.out
- Next Thread: Little help with RandomAccessFile
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






