如果我有这样的字符串:
String JSLines = "DefineEvent(20140208,'Starting of Study for (old and new) student's ','','',17,5)";
如何得到以下结果:
20140208|Starting of Study for (old and new) student's
我尝试这样做:
strDate = JSLines.Substring(JSLines.LastIndexOf("(") + 1, 8);
toolTip = JSLines.Substring(JSLines.IndexOf(',') + 2, (JSLines.IndexOf(',', JSLines.IndexOf(',') + 1)) - (JSLines.IndexOf(',') + 3));
但在我的第二个参数
中遇到'
或(
时失败了
答案 0 :(得分:3)
你可以试试这个
var substring = string.Join("|", "".Split(',').Take(2).Select(x => x.Trim('\'')));
当然,只有当字符串不包含,
时,这才有效,但在这种情况下,它们不是
答案 1 :(得分:2)
您只能使用字符串方法:
String JSLines = "DefineEvent(20140208,'Starting of Study for (old and new) student's ','','',17,5)";
string result = JSLines;
int methodBodyStart = JSLines.IndexOf("DefineEvent(");
if (methodBodyStart >= 0)
{
methodBodyStart += "DefineEvent(".Length;
int methodBodyEnd = JSLines.LastIndexOf(')');
if (methodBodyEnd >= 0)
{
string methodBody = JSLines.Substring(methodBodyStart, methodBodyEnd - methodBodyStart);
var twoParams = methodBody.Split(',')
.Select(str => str.Trim(' ', '\'')).Take(2);
result = string.Join("|", twoParams);
}
}
答案 2 :(得分:0)
你可以试试这个
var str = "DefineEvent(20140208,'Starting of Study for (old and new) student's ','','',17,5)";
var arr = str.Split(new[] {",'","',", "DefineEvent(" }, StringSplitOptions.None);
var result = String.Join("|", new[] {arr[1], arr[2]});