Solved

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

Posted on 2003-10-28
4
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

733 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