Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

if i pass a string (verstring == "vername.1.19.5") it will return the version because i am ignoring Currentver[0]. if i want to pass verstring == "1.19.5".

I mean i will get verstring in both formats with version name(vername) or only version(1.19.5)

C#
public VerInfo(string verString)
        {
            string[] currentVer;

            if (versionString.Contains("."))
                currentVer= versionString.Split(".".ToCharArray());
            else
                currentVer= versionString.Split(":".ToCharArray());

            a= Convert.ToByte(currentVer[1]);
            b= Convert.ToByte(currentVer[2]);
            c= Convert.ToByte(currentVer[3]);
        }


What I have tried:

I have tried using the string length, here string length vary's


if (versionString.Length > 5)
Posted
Updated 9-Mar-17 18:44pm
v2

You could use regular expressions, via the Regex class.
Try, for instance
C#
public static void Main()
{
  string[] verstring = new string[] { "vername.1.19.5", "1.19.5" };

  Regex r = new Regex(@"(?:vername\.?:)?(\d+)\.(\d+)\.(\d+)");
  for (int n = 0; n < 2; ++n)
  {
    System.Console.Write("Input string: '{0}', extracted numbers: ", verstring[n]);
    Match m = r.Match(verstring[n]);
    if (m.Success)
    {
      for (int i = 0; i < 3; ++i)
        System.Console.Write("'{0}' ", m.Groups[i + 1].Captures[0]);
      System.Console.WriteLine();
    }
  }
}


Output:
Input string: 'vername.1.19.5', extracted numbers: '1' '19' '5' 
Input string: '1.19.5', extracted numbers: '1' '19' '5' 
 
Share this answer
 
Comments
Member 12277263 9-Mar-17 17:10pm    
thanks
Is there way by using split we can extract that number.
Hi Member 12277263,

Your problem is basically to understand if currentVer[0] is a string ('vername') or not, and according to that parse the rest of the version information.

A quick workaround is:

1) Check if the first index is a string ('vername').
2) According to that set the parsing index starter - if it should start from 0, or 1.
public void VerInfo(string verString)
{
    string[] currentVer;

    if (verString.Contains("."))
        currentVer = verString.Split(".".ToCharArray());
    else
        currentVer = verString.Split(":".ToCharArray());

    int Index = 0;
    if (currentVer[0].ToLower().Equals("vername"))
        Index = 1;
    else Index;

    byte a = Convert.ToByte(currentVer[Index++]);
    byte b = Convert.ToByte(currentVer[Index++]);
    byte c = Convert.ToByte(currentVer[Index]);
}
You can further refine the IF-ELSE statement by replacing with ternary operator. I didn't do that for sake of clarity.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900