Click here to Skip to main content
Rate this: bad
good
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.4K
Edited 13-Dec-12 21:25pm
v2
Comments
Krunal Rohit at 14-Dec-12 3:20am
   
any efforts so far ?? Code ??
ntitish at 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 at 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 at 14-Dec-12 3:28am
   
sir it is saying that have so many charterers in a literal
ntitish at 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 at 14-Dec-12 8:24am
   
thanks bro for spending valuable time for me............
ntitish at 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 at 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 at 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 at 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 at 14-Dec-12 4:13am
   
I wonder why they didn't make an overload for the single string/character.
OriginalGriff at 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 at 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 at 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 at 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 at 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 at 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 at 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)



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