IndexOf returns -1 如果字符串包含“\”
IndexOf returns -1 if the string contains "\"
我有一个string code= "[=13=]\u0001[=13=][=13=][=13=]????\u0001[=13=][=13=][=13=][=13=][=13=][=13=][=13=]\u000f\u0001[=13=][=13=][=13=]\u001f\u0001\ABC01[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]\u0002\DEF[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]\u0003\\GHI01[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]"
我需要检索 u0001 和 u0002 以及 u0002 和 u0003 之间的数据等等。
输出为:
ABC,DEF, GHI etc.
我是如何努力实现它的:
code.Substring((code.IndexOf("\u000" + i) + ("\u000" + i).Length), code.IndexOf("\u000" + (i + 1)) - code.IndexOf("\u000" + i) - ("\u000" + i).Length));
这会导致编译错误:
Unrecognised escape sequence.
我试过 code.IndexOf("\u0001")
,这个有效但无效 code.IndexOf("\u000"+i)
。
如何解决?
编辑:你们中的许多人似乎都答错了问题,所以这里是完整的代码:
私有静态列表 RetriveMethod()
{
input="\u0001[=12=]\u0005[=12=][=12=][=12=]\u0001[=12=][=12=][=12=]\tMyMethod1\u0001\u001cfunction MyMethod1("there cud be another function by name function here" ) {\n\t\n\n}[=12=][=12=][=12=][=12=]\u0002[=12=][=12=][=12=]\tMyMethod2\u0001?\u0001function MyMethod2( ) { }[=12=][=12=][=12=][=12=]\u0003[=12=][=12=][=12=]\tMyMethod3\u0001Ofunction MyMethod3( )
List<string> ExtactedMethods = new List<string>();
for (int i = 0; i <= 3; i++)
{
ExtactedMethods.Add(code.Substring((code.IndexOf("\u000" + i) + ("\u000"
+ i).Length), code.IndexOf("\u000" + (i + 1)) -
code.IndexOf("\u000" + i) - ("\u000" + i).Length));
}
return ExtactedMethods;
}
\u----
表示 Unicode 字符(因此前缀 u
)。 "\u000"
是无效的 Unicode 字符,会导致编译错误。
如果你不希望 \u
被视为 Unicode 字符,那么转义 \
字符
"\u"
@Immersive 建议修复以在源字符串
中转义 \u
string code= @"[=11=]\u0001\...."
阅读更多关于 verbatim string literals
Note: code.IndexOf ("\ u000" + i) does not work because you can not convert an integer to a string, and what IndexOf does is find the numeric position of that character in the string, instead you should try code.IndexOf ( "\ u000") + i where are you this is the correct way to add a value to the position (value returned by IndexOf), if that is your goal
You can not use the backslash \ because it is an escape sequence character \ n \ r \ t, if you want to apply it, you must add two \ where the IDE will interpret that the first is the escape sequence and the second the character to read
无论如何,我已经创建了这个方法来提取文本字符串的大写字母
public List<string> GetUpperLetters(string input)
{
List<string> result = new List<string>();
int index_0 = 0;
string accum = string.Empty;
//Convert string input to char collection
//and walk it one by one
foreach (char c in input.ToCharArray())
{
if(char.IsLetter(c) && char.IsUpper(c))
{
accum += c;
index_0++;
if(index_0 == 3)
{
index_0 = 0;
result.Add(accum);
accum = string.Empty;
}
}
}
return result;
}
哪里
GetUpperLetters(code)[index from zero to n]
string code= "[=11=]\u0001[=11=][=11=][=11=]????\u0001[=11=][=11=][=11=][=11=][=11=][=11=][=11=]\u000f\u0001[=11=][=11=][=11=]\u001f\u0001\ABC01[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]\u0002\DEF[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]\u0003\\GHI01[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]";
GetUpperLetters(code)[0] returns ABC
GetUpperLetters(code)[1] returns DEF
GetUpperLetters(code)[2] returns GHI
非常感谢大家提供的建议答案。
@Immersive 的那个成功了!
要做的改动是string code= @"[=14=]\u0001[=14=][=14=][=14=]?????\u0001[=15=][=15=][=15=][=15=][=15=][=15=][=15=]\u000f\u0001[=15=][=15=][=15=]\u001f\u0001\ABC01[=15=][=15=][=15=]\u001f[=15=][=15=][=15=]\u0002\DEF[=15=][=15=][=15=]\u001f[=15=][=15=][=15=]\u0003\\GHI01[=16=][=16=][=16=]\u001f[=16=][=16=][=16=]"
和
code.Substring((code.IndexOf(@"\u000" + i) + (@"\u000" + i).长度), code.IndexOf(@"\u000 " + (i + 1)) - code.IndexOf(@"\u000" + i) - (@"\u000" + i).长度));
我有一个string code= "[=13=]\u0001[=13=][=13=][=13=]????\u0001[=13=][=13=][=13=][=13=][=13=][=13=][=13=]\u000f\u0001[=13=][=13=][=13=]\u001f\u0001\ABC01[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]\u0002\DEF[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]\u0003\\GHI01[=13=][=13=][=13=]\u001f[=13=][=13=][=13=]"
我需要检索 u0001 和 u0002 以及 u0002 和 u0003 之间的数据等等。
输出为:
ABC,DEF, GHI etc.
我是如何努力实现它的:
code.Substring((code.IndexOf("\u000" + i) + ("\u000" + i).Length), code.IndexOf("\u000" + (i + 1)) - code.IndexOf("\u000" + i) - ("\u000" + i).Length));
这会导致编译错误:
Unrecognised escape sequence.
我试过 code.IndexOf("\u0001")
,这个有效但无效 code.IndexOf("\u000"+i)
。
如何解决?
编辑:你们中的许多人似乎都答错了问题,所以这里是完整的代码:
私有静态列表 RetriveMethod()
{
input="\u0001[=12=]\u0005[=12=][=12=][=12=]\u0001[=12=][=12=][=12=]\tMyMethod1\u0001\u001cfunction MyMethod1("there cud be another function by name function here" ) {\n\t\n\n}[=12=][=12=][=12=][=12=]\u0002[=12=][=12=][=12=]\tMyMethod2\u0001?\u0001function MyMethod2( ) { }[=12=][=12=][=12=][=12=]\u0003[=12=][=12=][=12=]\tMyMethod3\u0001Ofunction MyMethod3( )
List<string> ExtactedMethods = new List<string>();
for (int i = 0; i <= 3; i++)
{
ExtactedMethods.Add(code.Substring((code.IndexOf("\u000" + i) + ("\u000"
+ i).Length), code.IndexOf("\u000" + (i + 1)) -
code.IndexOf("\u000" + i) - ("\u000" + i).Length));
}
return ExtactedMethods;
}
\u----
表示 Unicode 字符(因此前缀 u
)。 "\u000"
是无效的 Unicode 字符,会导致编译错误。
如果你不希望 \u
被视为 Unicode 字符,那么转义 \
字符
"\u"
@Immersive 建议修复以在源字符串
中转义\u
string code= @"[=11=]\u0001\...."
阅读更多关于 verbatim string literals
Note: code.IndexOf ("\ u000" + i) does not work because you can not convert an integer to a string, and what IndexOf does is find the numeric position of that character in the string, instead you should try code.IndexOf ( "\ u000") + i where are you this is the correct way to add a value to the position (value returned by IndexOf), if that is your goal
You can not use the backslash \ because it is an escape sequence character \ n \ r \ t, if you want to apply it, you must add two \ where the IDE will interpret that the first is the escape sequence and the second the character to read
无论如何,我已经创建了这个方法来提取文本字符串的大写字母
public List<string> GetUpperLetters(string input)
{
List<string> result = new List<string>();
int index_0 = 0;
string accum = string.Empty;
//Convert string input to char collection
//and walk it one by one
foreach (char c in input.ToCharArray())
{
if(char.IsLetter(c) && char.IsUpper(c))
{
accum += c;
index_0++;
if(index_0 == 3)
{
index_0 = 0;
result.Add(accum);
accum = string.Empty;
}
}
}
return result;
}
哪里
GetUpperLetters(code)[index from zero to n]
string code= "[=11=]\u0001[=11=][=11=][=11=]????\u0001[=11=][=11=][=11=][=11=][=11=][=11=][=11=]\u000f\u0001[=11=][=11=][=11=]\u001f\u0001\ABC01[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]\u0002\DEF[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]\u0003\\GHI01[=11=][=11=][=11=]\u001f[=11=][=11=][=11=]";
GetUpperLetters(code)[0] returns ABC
GetUpperLetters(code)[1] returns DEF
GetUpperLetters(code)[2] returns GHI
非常感谢大家提供的建议答案。 @Immersive 的那个成功了! 要做的改动是string code= @"[=14=]\u0001[=14=][=14=][=14=]?????\u0001[=15=][=15=][=15=][=15=][=15=][=15=][=15=]\u000f\u0001[=15=][=15=][=15=]\u001f\u0001\ABC01[=15=][=15=][=15=]\u001f[=15=][=15=][=15=]\u0002\DEF[=15=][=15=][=15=]\u001f[=15=][=15=][=15=]\u0003\\GHI01[=16=][=16=][=16=]\u001f[=16=][=16=][=16=]" 和
code.Substring((code.IndexOf(@"\u000" + i) + (@"\u000" + i).长度), code.IndexOf(@"\u000 " + (i + 1)) - code.IndexOf(@"\u000" + i) - (@"\u000" + i).长度));