Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the string = ("\"20v2015-04-13\"")


i need it in the form of

string strvalue= 20;
string strDate=2015-04-13;

How is it possible?
Posted

Use a regex:
(?<value>\d+)[a-zA-Z](?<date>\d{4}-\d\d-\d\d)</date></value>
Should do it - it extracts the data into two groups "Value" and "Date" which you can pick up without the remaining rubbish.
 
Share this answer
 
Comments
aiswarjya 14-Mar-15 5:37am    
I write it in my .cs file. how can i put this?
OriginalGriff 14-Mar-15 5:57am    
You know how to use a Regex, don't you? Or have they not covered that on your course yet?
C#
//Use substring expects arg starting length
string str = ("\"20v2015-04-13\"")
//for your ans try this
string strDate = str.Substring(5, 10); 

//OR
//see below link


http://csharp.net-informations.com/string/files/print/csharp-string-substring_print.htm[^]

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. 
 
Share this answer
 
v2
In complement to solution 1, which gives you an appropriate regular expression for your requirement, here is the way to use a regular expression in C#:
C#
using System.Text.RegularExpressions;

string original = "20v2015-04-13";
Regex r = new Regex(@"(?<value>\d+)[a-zA-Z](?<date>\d{4}-\d\d-\d\d)", RegexOptions.Compiled | RegexOptions.CultureInvariant);
Match m = r.Match(original);
string value = String.Empty;
string date = String.Empty;
if (m.Success) {
   value = m.Groups["value"].Value;
   date = m.Groups["date"].Value;
}


More on regular expressions in .NET here:
System.Text.RegularExpressions Namespace[^]
 
Share this answer
 
v2
XML
<b> first Approach:</b>

 string mainstring = "20v2015-04-13";
 string[] separatevalue = mainstring .Split('v');
 string first =separatevalue[0];
 string second=separatevalue[1];


<b>second Approach:</b>

 string mainstring = "20v2015-04-13";
 string first = mainstring .Substring(0, 2);
 string second = mainstring .Substring(3, 10);
 
Share this answer
 
v2
use this :

string mainstring = "20v2015-04-13";
string[] ALL = Regex.Split(mainstring , "v");
string first =ALL[0];
string second=ALL[1];
 
Share this answer
 
v2
Try this:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string str = ("\"20v2015-04-13\"");
        Match match = Regex.Match(str, @"\d{4}-\d{2}-\d{2}|\d{2}");
        while (match.Success)
        {
            Console.WriteLine(match.Value);
            match = match.NextMatch();
        }
    }
}
 
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