Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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