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

i have a value like this 1:3 want to split this value using asp.net and c#, i have two varaiables a,b want to pass those split values to these variables.


Thanks
Posted

You can use split[^] method to do this.

Assuming you want to split 1:3 to 1 and 3, here is a sample:
C#
string strData = "1:3";
string[] strSplitResult = strData.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


 string a = "";
 string b = "";

 if (strSplitResult.Length > 1)
 {
     a = strSplitResult[0];
     b = strSplitResult[1];
 }
 
Share this answer
 
v2
In C#
C#
string a, b;
            string no = "1:3";
             a = no.Split(':')[0];
             b = no.Split(':')[1];
 
Share this answer
 
Comments
p-arthi 3-Sep-12 2:13am    
mine 5+
D-Kishore 3-Sep-12 2:14am    
Thanks
Assuming it is in a string:
C#
string s = "1:3";
string[] parts = s.Split(':');
int a, b;
if (parts.Length >= 2)
    {
    if (int.TryParse(parts[0], out a) && int.TryParse(parts[1], out b))
        {
        ...
        }
    }
 
Share this answer
 
C#
string s="1:3";
int a,b;
string[] sSplit = s.Split(':');
a = Convert.ToInt32(sSplit[0]);
b = Convert.ToInt32(sSplit[1]);
 
Share this answer
 
Use Split on your string : http://www.dotnetperls.com/split[^]
 
Share this answer
 
You can use Split[^] function of string object.
Try this:
C#
int a = Convert.ToInt32(string.Format("1:3").Split(':')[0].Trim());
int b = Convert.ToInt32(string.Format("1:3").Split(':')[1].Trim());



--Amit
 
Share this answer
 
C#
string s = "1:3";
    
    string[] words = s.Split(':');
int a,b;
if(words.Length>1)
{
   a = Convert.ToInt32(words[0]);
   b = Convert.ToInt32(words[1]);
}
 
Share this answer
 
v2
hi,

http://www.dotnetperls.com/split

Review this link.
you will get all the idea about Split.

Thanks,
Viprat
 
Share this answer
 
For example:
Contents of input file (TextFile1.txt)

Dog,Cat,Mouse,Fish,Cow,Horse,Hyena
Programmer,Wizard,CEO,Rancher,Clerk,Farmer

C#
using System;
using System.IO;

class Program
{
    static void Main()
    {
    int i = 0;
    foreach (string line in File.ReadAllLines("TextFile1.txt"))
    {
        string[] parts = line.Split(',');//Spliter can be , : //
        foreach (string part in parts)
        {
        Console.WriteLine("{0}",part);
        }        
    }
    }
}


Output:

VB
Dog
Cat
Mouse
Fish
Cow
Horse
Hyena
Programmer
Wizard
CEO
Rancher
Clerk
Farmer
 
Share this answer
 
C#
string sKS = dtUserDetails.Rows[u][16].ToString().Trim();
                       char[] cDelchar = { ',' };
                       string[] sKsIDs = sKS.Split(cDelchar);
                       for (int kj = 0; kj < sKsIDs.Length; kj++)
                       {
                           string sGetKSId = sKsIDs[kj];


}
 
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