C# 如何从字符串中获取一些字符
我有一根绳子C# 如何从字符串中获取一些字符,c#,string,C#,String,我有一根绳子 string str = "2952885619_Table!$1$4"; 我想提取值1和4 我试过了 int index =str.IndexOf('$'); if (index > 0) { int rowIndex = Convert.ToInt32(string.Substring(index, index + 1)); int colIndex = Convert.ToInt32(BookmrkID.Substring(index, index +
string str = "2952885619_Table!$1$4";
我想提取值1和4
我试过了
int index =str.IndexOf('$');
if (index > 0)
{
int rowIndex = Convert.ToInt32(string.Substring(index, index + 1));
int colIndex = Convert.ToInt32(BookmrkID.Substring(index, index + 3));
}
但它抛出了一个系统。ArgumentOutOfRangeException
,我不知道为什么
有其他方法提取这两个值吗?您可以从
$
开始,使用string.Substring
和string.IndexOf
获取字符串,然后您可以在$
上拆分字符串并删除空条目。这将为您提供1
和4
作为字符串值,稍后您可以将它们解析为int
,如:
List<int> list = str.Substring(str.IndexOf('$'))
.Split(new[] { '$' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List List=str.Substring(str.IndexOf('$'))
.Split(新[]{'$'},StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
您的
列表将包含1
和4
作为整数值 您可以使用string.Substring
和string.IndexOf
从$
开始获取字符串,然后您可以在$
上拆分字符串并删除空条目。这将为您提供1
和4
作为字符串值,稍后您可以将它们解析为int
,如:
List<int> list = str.Substring(str.IndexOf('$'))
.Split(new[] { '$' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List List=str.Substring(str.IndexOf('$'))
.Split(新[]{'$'},StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
您的列表将包含1
和4
作为整数值 您可以使用string.Substring
和string.IndexOf
从$
开始获取字符串,然后您可以在$
上拆分字符串并删除空条目。这将为您提供1
和4
作为字符串值,稍后您可以将它们解析为int
,如:
List<int> list = str.Substring(str.IndexOf('$'))
.Split(new[] { '$' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List List=str.Substring(str.IndexOf('$'))
.Split(新[]{'$'},StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
您的列表将包含1
和4
作为整数值 您可以使用string.Substring
和string.IndexOf
从$
开始获取字符串,然后您可以在$
上拆分字符串并删除空条目。这将为您提供1
和4
作为字符串值,稍后您可以将它们解析为int
,如:
List<int> list = str.Substring(str.IndexOf('$'))
.Split(new[] { '$' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List List=str.Substring(str.IndexOf('$'))
.Split(新[]{'$'},StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
您的列表将包含1
和4
作为整数值 这里是另一种方法:
var splits = str.Split('$');
var rowIndex = splits[1];
var colIndex = splits[2];
以下是另一种方法:
var splits = str.Split('$');
var rowIndex = splits[1];
var colIndex = splits[2];
以下是另一种方法:
var splits = str.Split('$');
var rowIndex = splits[1];
var colIndex = splits[2];
以下是另一种方法:
var splits = str.Split('$');
var rowIndex = splits[1];
var colIndex = splits[2];
string.Substring调用的第二个参数是长度,而不是结束范围。您必须计算项目在字符串中的位置差:
if (index > 0){
int rowIndex = Convert.ToInt32(string.Substring(index, 1));
int colIndex = Convert.ToInt32(BookmrkID.Substring(index + 2, 1));
}
Doc:字符串的第二个参数。子字符串调用是长度而不是结束范围。您必须计算项目在字符串中的位置差:
if (index > 0){
int rowIndex = Convert.ToInt32(string.Substring(index, 1));
int colIndex = Convert.ToInt32(BookmrkID.Substring(index + 2, 1));
}
Doc:字符串的第二个参数。子字符串调用是长度而不是结束范围。您必须计算项目在字符串中的位置差:
if (index > 0){
int rowIndex = Convert.ToInt32(string.Substring(index, 1));
int colIndex = Convert.ToInt32(BookmrkID.Substring(index + 2, 1));
}
Doc:字符串的第二个参数。子字符串调用是长度而不是结束范围。您必须计算项目在字符串中的位置差:
if (index > 0){
int rowIndex = Convert.ToInt32(string.Substring(index, 1));
int colIndex = Convert.ToInt32(BookmrkID.Substring(index + 2, 1));
}
Doc:只要知道只有一个数字,就需要将子字符串的长度(第二个参数)设置为1:
如果您不知道数字的数量,则需要做更多的工作:
int index1 = str.IndexOf('$');
if (index1 > 0) {
int index2 = str.IndexOf('$', index1 + 1);
if (index2 > 0) {
int rowIndex = Convert.ToInt32(str.Substring(index1 + 1, index2 - index1 - 1));
int colIndex = Convert.ToInt32(str.Substring(index2 + 1, str.Length - index2 - 1));
}
}
当然,其他人发布的策略也会起作用。只要知道只有一个数字,就需要将子字符串的长度(第二个参数)设置为1:
如果您不知道数字的数量,则需要做更多的工作:
int index1 = str.IndexOf('$');
if (index1 > 0) {
int index2 = str.IndexOf('$', index1 + 1);
if (index2 > 0) {
int rowIndex = Convert.ToInt32(str.Substring(index1 + 1, index2 - index1 - 1));
int colIndex = Convert.ToInt32(str.Substring(index2 + 1, str.Length - index2 - 1));
}
}
当然,其他人发布的策略也会起作用。只要知道只有一个数字,就需要将子字符串的长度(第二个参数)设置为1:
如果您不知道数字的数量,则需要做更多的工作:
int index1 = str.IndexOf('$');
if (index1 > 0) {
int index2 = str.IndexOf('$', index1 + 1);
if (index2 > 0) {
int rowIndex = Convert.ToInt32(str.Substring(index1 + 1, index2 - index1 - 1));
int colIndex = Convert.ToInt32(str.Substring(index2 + 1, str.Length - index2 - 1));
}
}
当然,其他人发布的策略也会起作用。只要知道只有一个数字,就需要将子字符串的长度(第二个参数)设置为1:
如果您不知道数字的数量,则需要做更多的工作:
int index1 = str.IndexOf('$');
if (index1 > 0) {
int index2 = str.IndexOf('$', index1 + 1);
if (index2 > 0) {
int rowIndex = Convert.ToInt32(str.Substring(index1 + 1, index2 - index1 - 1));
int colIndex = Convert.ToInt32(str.Substring(index2 + 1, str.Length - index2 - 1));
}
}
当然,其他人发布的策略也会起作用。这里是您命名的变量
string str = "2952885619_Table!$1$4";
这里调用变量string和bookmrkID上的函数substring
Convert.ToInt32(string.Substring(index, index + 1));
试一试
introwindex=Convert.ToInt32(str.Substring(index,1)) 您在这里命名了变量
string str = "2952885619_Table!$1$4";
这里调用变量string和bookmrkID上的函数substring
Convert.ToInt32(string.Substring(index, index + 1));
试一试
introwindex=Convert.ToInt32(str.Substring(index,1)) 您在这里命名了变量
string str = "2952885619_Table!$1$4";
这里调用变量string和bookmrkID上的函数substring
Convert.ToInt32(string.Substring(index, index + 1));
试一试
introwindex=Convert.ToInt32(str.Substring(index,1)) 您在这里命名了变量
string str = "2952885619_Table!$1$4";
这里调用变量string和bookmrkID上的函数substring
Convert.ToInt32(string.Substring(index, index + 1));
试一试
introwindex=Convert.ToInt32(str.Substring(index,1)) 第二个参数描述如下:
长度
类型:System.Int32
子字符串中的字符数
您分别为第二个参数传递index+1
和index+3
,而这可能类似于