Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone let me know about the following scenario?

ANYSTRING_ON_LEFT#ANYSTRING_ON_RIGHT

If I have any set of string on the left of # and any set of string on the right of #, then, the left part should be saved in one string var and the right string should be stored in another string var.
Posted
Updated 22-Oct-10 3:55am
v3
Comments
Hiren solanki 22-Oct-10 8:56am    
ask google, 'string split + csharp" :)
Dalek Dave 22-Oct-10 9:55am    
Minor Edit for Readability.

Using strings.split[^] and then store into vars.
 
Share this answer
 
Comments
Omar Akhtar 2009 22-Oct-10 8:38am    
Kindly tell what you mean by the sign "^", within square brackets.
Goutam Patra 22-Oct-10 8:52am    
Thats a new window link
Personally, I think showing you String.Split does you a dis-service. Clearly you are new to coding and you should be forced to think through the steps on your own and then figure out how to code it yourself. That's the only real way that you will learn. Then, you can use the shortcuts and methods that have already been created to help you.

In your case, you should ask yourself what you are trying to do. How does your brain do it? Then ask yourself what a string is. (And this is where not getting formal training hurts you). Anyone starting with an introductory c++ class learns that a string is really just an array of chars. And you can access each char individually just as you would an array. So, the first char can be accessed using myString[0].

So, how would you do it without String.Split...or how does String.Split work?

If you know there will only ever be one '#' in your string, then it's pretty simple. You can create two variables: firstPart, secondPart. Then, iterate through the chars in the array and add each one to firstPart until you hit the '#'. Then, add each char after to secondPart.
 
Share this answer
 
Comments
William Winner 22-Oct-10 12:58pm    
If you're going to vote me a one, you should have the courage to at least say why...
use the below code to split the string

string temp = "ANYSTRING_ON_LEFT#ANYSTRING_ON_RIGHT";
string[] strs = temp.Split(new char[] { '#' });



then pass the spited values to your variables.
 
Share this answer
 
Comments
Omar Akhtar 2009 22-Oct-10 8:40am    
But how to pass? that is the question I am asking, although I have tried the same line of code before. I just want to know how to store the left part and right part.
Rajesh Anuhya 22-Oct-10 9:00am    
see the below answer
William Winner 22-Oct-10 11:58am    
gave you a 5 vote because your answer is correct and shouldn't be downvoted.
string myStr = @"left#right";
string [] myArr = new string[];
myArr = string.Split('#');
Console.WriteLine("Left: " + myArray[0]); //myArray[0] contains left part

Console.WriteLine("Right: " + myArray[1]); //myArray[1] contains right part
 
Share this answer
 
v2
When you split the string, you end up with an aray of the various parts that were separated at the specified character. None of the parts that are created contain the split character itself. You can iterate through the resulting array using foreach (or any other hnumber of looping mechanisms in the language of choice).

C#
string temp = "abc#xyz";
string[] parts = temp.Split('#');
foreach(string part in parts)
{
    Console.WriteLine(part);
}
 
Share this answer
 
to Question Poster..,
am sorry to say that., i think you are familiar with string array. That is a simple as abhinav said.

And why i am not mentioned is i don't know how your sting will in real time(may be more than one "#").
 
Share this answer
 
Comments
Omar Akhtar 2009 22-Oct-10 10:44am    
Thanks for your kind reply. Let me clear you a little more, there will be only one # sign in between. Moreover, because I am not aware of the 0 and 1 that abhinav has told me earlier thats why I am requesting for myself a little guidance. Kindly, let me know regarding my probelm as per your programmatic answer is concerned.

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