Solved

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

Posted on 2003-10-28
4
214 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 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