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

Below is my string in a notepad

PUCID: 0000000000000002:LastPBLine: 7


Now i want to extract the number's and assign to variables.

e.g. a=0000000000000002
and b=7

Please tell me how to do this
Posted

The simplest way which you can try is as follows

C#
string text = System.IO.File.ReadAllText(@"C:\Users\UserName\Documents\Work\test.txt");
            string[] arr=text.Split(':');
            List<mymodel> modellist = new List<mymodel>();

            Dictionary<string,> d_list = new Dictionary<string,>();
            for (int i = 0; i < arr.Length; i = i + 2)
            {
                MyModel mm = new MyModel();
                mm.FirstNum = arr[i];
                mm.SecondNum = arr[i + 1];
                modellist.Add(mm);
            }

</mymodel></mymodel>


now you can easily iterate over
C#
modellist
 
Share this answer
 
you can get it easily if you have lenth of string and start index by substring:

C#
string myStr ="this is test";
//string.Substring(int startInde,int length);
string myPart = myStr .Substring(2,7);//the result is:"s is te"


and if you have a specefic character:
C#
string myStr ="this*is*test";
string[] myPart = myStr.Split('*');
//the result is:
//myPart[0] = "this";
//myPart[1] = "is";
//myPart[2] = "test";
 
Share this answer
 
Try this:
C#
string[] parts = yourStr.Split(':');
string a = parts[1].Trim();
string b = parts[3].Trim();
 
Share this answer
 
Comments
[no name] 3-Jul-14 4:56am    
Thanks..it worked..:)
Thomas Daniels 3-Jul-14 5:11am    
You're welcome!

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