tags:

views:

1376

answers:

6

Hi,

I'm building a page and would like to know how to extract substring from a string until finds a comma in asp.net c#. Can someone help please?

Thanks,

Alina

+1  A: 
myString = myString.Substring(0,myString.IndexOf(','));
Ian Devlin
shouldn't that be myString.IndexOf ?
dove
Oops! Yes, thanks! :-)
Ian Devlin
It will also throw an ArgumentOutOfRange exception if the string contains no comma.
Vinko Vrsalovic
I'm leading him down the right path, not writing the entire solution for him!
Ian Devlin
And I'm completing your answer.
Vinko Vrsalovic
+2  A: 
string NoComma = "";
string example = "text before first comma, more stuff and another comma, there";
string result = example.IndexOf(',') == 0 ? NoComma  : example.Split(',')[0];
dove
Why not just use example.Split(',')[0] by itself?
thecoop
@thecoop this would be if you wanted control over what to result if there was no comma in the example, question wasn't explicit on this. amazing how much variance you can get on the most trivial of requirements...
dove
thanks, how can i do with multiple commas? I want to save all the words between the commas.Alina
alina
@alina string[] results = example.Split(','); again you need to decide how to deal with edge cases, e.g. no commas, comma as first char, etc
dove
+8  A: 
substring = str.Split(',')[0];

If str doesn't contain any commas, substring will be the same as str.

EDIT: as with most things, performance of this will vary for edge cases. If there are lots and lots of commas, this will create lots of String instances on the heap that won't be used. If it is a 5000 character string with a comma near the start, the IndexOf+Substring method will perform much better. However, for reasonably small strings this method will work fine.

thecoop
+1 the most elegant.
dove
Yes, the key is not needing to check anything.
Vinko Vrsalovic
+1 from me too. Nice solution.
Ian Devlin
Note that this is incredibly inefficient for large strings that contain a comma near their beginning. `String.IndexOf` will stop when it finds the first comma. `String.Split` will always go through the _entire_ string. Consider "Hello, world! I have a very long tale to tell you." + [6000 more characters] + "Hope you enjoyed it!". That's a huge slowdown factor you have there.
Joren
Lotfi
Indeed, the sheer amount of possible allocations is another performance problem.
Joren
There are many potential performance pitfalls, correct. But if and how severely will the code suffer from them depend heavily on usage, so while I totally agree with mentioning the problems, I wouldn't discard it as a valid solution right outfront. All that summed to the fact that, given the comment of the OP below in the answer to dove, [s]he actually wants to use Split. :-)
Vinko Vrsalovic
You're right. The question should be edited then.
Joren
+4  A: 

You can use IndexOf() to find out where is the comma, and then extract the substring. If you are sure it will always have the comma you can skip the check.

        string a = "asdkjafjksdlfm,dsklfmdkslfmdkslmfksd";
        int comma = a.IndexOf(',');
        string b = a;
        if (comma != -1)
        {
            b = a.Substring(0, comma);
        }
        Console.WriteLine(b);
Vinko Vrsalovic
A: 

Alina, based on what you wrote above, then Split will work for you.

string[] a = comment.Split(',');

Given your example string, then a[0] = "aaa", a[1] = "bbbbb", a[2] = "cccc", and a[3] = "dddd"

Ian Devlin
Hi Ian,Thanks, this helped me :)
alina
Glad to be of help! (please mark my answer as the correct one then :-))
Ian Devlin
+1  A: 
var firstPart = str.Split(',', 2)[0]

Second parameter tells maximum number of parts. Specifying 2 ensures performance is fine even if there are lots and lots of commas.

Konstantin Spirin