Solved

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

Posted on 2003-10-28
4
213 Views
Last Modified: 2010-04-16
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
Comment
Question by:jpchen
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
_ys_ earned 125 total points
ID: 9633128
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

by:jpchen
ID: 9636267
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

by:jpchen
ID: 9636460
I found out what problem is.
Thanks!
jpc
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 9649352
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now