Click here to Skip to main content
12,253,990 members (64,448 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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
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 13-Dec-12 21:18pm
ntitish1.9K
Edited 13-Dec-12 21:25pm
v2
Comments
Krunal Rohit 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
ntitish 14-Dec-12 8:24am
   
thanks bro for spending valuable time for me............
ntitish 14-Dec-12 8:24am
   
at last i got the answer
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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'.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Change the quotes to double quotes:
string jobposting = (jobHeading.Split('in'))[0];
 
string companyname = (jobHeading.Split('in'))[1];
Becomes
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:
string[] parts = jobHeading.Split(" in ");
if (parts.Length == 2)
   {
   string jobposting = parts[0]; 
   string companyname = parts[1];
   ...
   }
  Permalink  
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
OriginalGriff 14-Dec-12 5:00am
   
I think you typed it wrong - show the code for line 104, I think you mistyped "(" and ")" for "{" and "}"
ntitish 14-Dec-12 5:33am
   
string[] parts = jobHeading.Split(new string[]( " in " ),StringSplitOptions.None);
if (parts.Length == 2)
{
string jobposition = parts[0];
string companyname = parts[1];
}
OriginalGriff 14-Dec-12 5:36am
   
Look at the original code, then at yours.
See the curly brackets in mine? See you have replaced them with round brackets?
(they are the ones around " in " if you can't see it - you have
(" in ")
I have
{" in "}
ntitish 14-Dec-12 5:40am
   
thanks a lot bro...........at last i got it.
how intelligent you people are.....i am the fresher for this job .thanks a lot
OriginalGriff 14-Dec-12 5:48am
   
You're welcome!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100