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?
我正在构建一个页面,我想知道如何从字符串中提取子字符串,直到在asp.net c#中找到一个逗号。有人能帮吗?
Thanks,
谢谢,
Alina
爱丽娜
6 个解决方案
#1
37
substring = str.Split(',')[0];
If str doesn't contain any commas, substring will be the same as str.
如果str不包含任何逗号,子字符串将与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.
编辑:和大多数情况一样,对于边缘情况,性能也会有所不同。如果有大量的逗号,这将在堆上创建许多不被使用的字符串实例。如果它是一个5000个字符的字符串,在开头附近有一个逗号,那么IndexOf+子字符串方法的性能会更好。但是,对于相当小的字符串,这个方法会很好。
#2
11
var firstPart = str.Split(new [] { ',' }, 2)[0]
Second parameter tells maximum number of parts. Specifying 2 ensures performance is fine even if there are lots and lots of commas.
第二个参数表示零件的最大数量。指定2可以确保性能良好,即使有很多逗号。
#3
8
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.
您可以使用IndexOf()查找逗号,然后提取子字符串。如果你确定它总是有逗号,你可以跳过检查。
string a = "asdkjafjksdlfm,dsklfmdkslfmdkslmfksd";
int comma = a.IndexOf(',');
string b = a;
if (comma != -1)
{
b = a.Substring(0, comma);
}
Console.WriteLine(b);
#4
8
myString = myString.Substring(0,myString.IndexOf(','));
#5
4
Alina, based on what you wrote above, then Split will work for you.
Alina,根据你上面写的,然后Split将对你有效。
string[] a = comment.Split(',');
Given your example string, then a[0] = "aaa", a[1] = "bbbbb", a[2] = "cccc", and a[3] = "dddd"
给出示例字符串,然后是[0]= "aaa", [1] = "bbbbb", [2] = "cccc", [3] = "dddd"
#6
3
string NoComma = "";
string example = "text before first comma, more stuff and another comma, there";
string result = example.IndexOf(',') == 0 ? NoComma : example.Split(',')[0];
#1
37
substring = str.Split(',')[0];
If str doesn't contain any commas, substring will be the same as str.
如果str不包含任何逗号,子字符串将与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.
编辑:和大多数情况一样,对于边缘情况,性能也会有所不同。如果有大量的逗号,这将在堆上创建许多不被使用的字符串实例。如果它是一个5000个字符的字符串,在开头附近有一个逗号,那么IndexOf+子字符串方法的性能会更好。但是,对于相当小的字符串,这个方法会很好。
#2
11
var firstPart = str.Split(new [] { ',' }, 2)[0]
Second parameter tells maximum number of parts. Specifying 2 ensures performance is fine even if there are lots and lots of commas.
第二个参数表示零件的最大数量。指定2可以确保性能良好,即使有很多逗号。
#3
8
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.
您可以使用IndexOf()查找逗号,然后提取子字符串。如果你确定它总是有逗号,你可以跳过检查。
string a = "asdkjafjksdlfm,dsklfmdkslfmdkslmfksd";
int comma = a.IndexOf(',');
string b = a;
if (comma != -1)
{
b = a.Substring(0, comma);
}
Console.WriteLine(b);
#4
8
myString = myString.Substring(0,myString.IndexOf(','));
#5
4
Alina, based on what you wrote above, then Split will work for you.
Alina,根据你上面写的,然后Split将对你有效。
string[] a = comment.Split(',');
Given your example string, then a[0] = "aaa", a[1] = "bbbbb", a[2] = "cccc", and a[3] = "dddd"
给出示例字符串,然后是[0]= "aaa", [1] = "bbbbb", [2] = "cccc", [3] = "dddd"
#6
3
string NoComma = "";
string example = "text before first comma, more stuff and another comma, there";
string result = example.IndexOf(',') == 0 ? NoComma : example.Split(',')[0];