Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to separate the string and save into two variables.but this value of string is coming from the table of combination of two fields.now i want to separate it into two fields
C#
string jobHeading = Session["selectedJobHeading"].ToString();

   //in jobHeading string the value will come like this(jobposition in companyname) here jobposition and comapanyname are fields

                    string jobposting = (jobHeading.Split('in'))[0];

                    string companyname = (jobHeading.Split('in'))[1];
Posted
Updated 13-Dec-12 21:25pm
v2
Comments
[no name] 14-Dec-12 3:20am    
any efforts so far ?? Code ??
ntitish 14-Dec-12 3:24am    
string jobHeading = Session["selectedJobHeading"].ToString();

//in jobHeading string the value will come like this(jobposition in companyname) here jobposition and comapanyname are fields

string jobposting = (jobHeading.Split('in'))[0];

string companyname = (jobHeading.Split('in'))[1];
ntitish 14-Dec-12 3:26am    
and i want to separate that two fields and want to save in two variables.
can u answer me fast sir.
ntitish 14-Dec-12 3:28am    
sir it is saying that have so many charterers in a literal
ntitish 14-Dec-12 3:51am    
string[] parts = jobHeading.Split(" in ");
if (parts.Length == 2)
{
string jobposting = parts[0];
string companyname = parts[1];
...
}


THIS WAS THE ERROR I AM GETTING AFTER APPLYING

Error 91 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments D:\nageswararao\dec-14-12-morning\Alumini_App\Alumini_App\jobpostdisplay.aspx.cs 104 38 Alumini_App

You may solve the problem only if one (or both) of the following conditions hold:
  1. The composing substrings are of fixed length.
  2. There is a separator (that is special character) between the composing substrings.
  3. There is another unique method for distinguishing the substrings (e.g. they belong to two different sets).


If condition 1 holds then use the String.Substring[^] method.

If condition 2 holds then use the String.Split[^] method.

If condition 3 holds then you should exploit the 'distinguishing method'.
 
Share this answer
 
Comments
ntitish 14-Dec-12 4:00am    
string[] parts = jobHeading.Split(" in ").ToString();
if (parts.Length == 2)
{
string jobposition = parts[0];
string companyname = parts[1];
}
after applying this i am getting this error
Error 91 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments D:\nageswararao\dec-14-12-morning\Alumini_App\Alumini_App\jobpostdisplay.aspx.cs 104 38 Alumini_App
Change the quotes to double quotes:
C#
string jobposting = (jobHeading.Split('in'))[0];

string companyname = (jobHeading.Split('in'))[1];
Becomes
C#
string jobposting = (jobHeading.Split("in"))[0];

string companyname = (jobHeading.Split("in"))[1];
But it would be better to do the split once, and check it, and to include the spaces in the split:
C#
string[] parts = jobHeading.Split(" in ");
if (parts.Length == 2)
   {
   string jobposting = parts[0]; 
   string companyname = parts[1];
   ...
   }
 
Share this answer
 
Comments
ntitish 14-Dec-12 3:53am    
string[] parts = jobHeading.Split(" in ");
if (parts.Length == 2)
{
string jobposting = parts[0];
string companyname = parts[1]; ...
}
THIS WAS THE ERROR I AM GETTING AFTER APPLYING

Error 91 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments D:\nageswararao\dec-14-12-morning\Alumini_App\Alumini_App\jobpostdisplay.aspx.cs 104 38 Alumini_App
OriginalGriff 14-Dec-12 4:07am    
Sorry, brain death has occurred - probably due to a lack of coffee...
string[] parts = jobHeading.Split(new string[] { " in " }, StringSplitOptions.None);
CPallini 14-Dec-12 4:13am    
I wonder why they didn't make an overload for the single string/character.
OriginalGriff 14-Dec-12 4:28am    
I know what you mean: I kept trying to use one, so I wrote an extension method - hence I got caught when I posted here... :doh:
ntitish 14-Dec-12 4:40am    
Error 32 Array creation must have array size or array initializer D:\nageswararao\dec-14-12-morning\Alumini_App\Alumini_App\jobpostdisplay.aspx.cs 104 65 Alumini_App
after applying what u said

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