Solved

# Take out the leading zero and 'F' form the string

Posted on 2003-10-28
212 Views
Hi All,
I'd like to have function to take out the string which has leading zero and 'F' character if appear but this function should only apply if the string is a number.
For example:
'F1123' ==> 1123
'F00012' ==> 12
'Fa12a' ==> Fa12a
'F0asd' ==> F0asd
'00123' ==> 123
'123AB' ==> 123AB
Thanks!
jpc
0
Question by:jpchen
• 2

LVL 9

Accepted Solution

_ys_ earned 125 total points
This should do the trick:

using System;

string foo ( string sInput )
{
char[] achExclude = {'f', 'F', '0'};

string sTrimmed = sInput.TrimStart (achExclude); // exclude the leading 'junk'

Int64 i64Result = 0;
try
{
i64Result = Int64.Parse (sTrimmed); // convert to a number
}
catch (Exception)
{
return sInput; // not a number - return original string
}

return i64Result.ToString( );
}
0

Author Comment

I change to this, but I always got 0, why?

private void button3_Click(object sender, System.EventArgs e)
{
char[] achExclude = {'f', 'F', '0'};

string oText = textBox1.Text;
string sTrimmed = textBox1.ToString().TrimStart (achExclude); // exclude the leading 'junk'

Int64 i64Result = 0;
try
{
i64Result = Int64.Parse (sTrimmed); // convert to a number
}
catch (Exception)
{
textBox1.Text = oText;
//                              return sInput; // not a number - return original string
}
textBox1.Text = i64Result.ToString( );
}
0

Author Comment

I found out what problem is.
Thanks!
jpc
0

LVL 14

Expert Comment

Even though the Q is already closed, you could have this with a one-liner:
using System.Text.RegularExpressions;

string trimmed=Regex.Match(input, @"^(F?0*(?<value>[1-9]\d*)|(?<value>.*))\$").Groups["value"].Value;
0

## Featured Post

Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.