繁体   English   中英

如果字符串包含“ \\”,则IndexOf返回-1

[英]IndexOf returns -1 if the string contains “\”

我有一个string code= "\\0\\\0\\0\\0????\\\0\\0\\0\\0\\0\\0\\0\\\\0\\0\\0\\\\\\ABC01\\0\\0\\0\\\0\\0\\0\\\DEF\\01\\0\\0\\0\\\0\\0\\0\\\\\\\GHI01\\0\\0\\0\\\0\\0\\0"

我需要检索u0001和u0002与u0002和u0003之间的数据,依此类推。

输出为:

ABC,DEF,GHI等

我如何实现它:

code.Substring((code.IndexOf("\u000" + i) + ("\u000" + i).Length), code.IndexOf("\u000" + (i + 1)) - code.IndexOf("\u000" + i) - ("\u000" + i).Length));

这导致编译错误:

无法识别的转义序列。

我试过了code.IndexOf("\") ,但没有code.IndexOf("\\u000\u0026quot;+i)

怎么解决?

编辑:你们中的许多人似乎都把问题弄错了,所以这里是完整的代码:

私有静态列表RetriveMethod()

    {            
         input="\u0001\0\u0005\0\0\0\u0001\0\0\0\tMyMethod1\u0001\u001cfunction MyMethod1("there cud be another function by name function here" ) {\n\t\n\n}\0\0\0\0\u0002\0\0\0\tMyMethod2\u0001?\u0001function MyMethod2( ) { }\0\0\0\0\u0003\0\0\0\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\u0026quot;是无效的Unicode字符,会导致编译错误。

如果您不想将\\u\u003c/code>视为Unicode字符,则应转义\\字符

"\\u"

@Immersive建议的修复程序,以在源字符串中转义\\u\u003c/code>

string code= @"\0\u0001\...."

阅读有关逐字字符串文字的更多信息

注意:code.IndexOf(“ \\ u000” + i)不起作用,因为您无法将整数转换为字符串,而IndexOf所做的是找到该字符在字符串中的数字位置,而应尝试使用code.IndexOf (“ \\ u000”)+ i如果您的目标是向位置添加值(由IndexOf返回的值),那么这是正确的方法

您不能使用反斜杠\\,因为它是一个转义序列字符\\ n \\ r \\ t,如果要应用反斜杠\\ n,则必须添加两个\\\\,IDE会在其中将第一个解释为转义序列,将第二个解释为要读取的字符

无论如何,我都创建了此方法来提取文本字符串的大写字母

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)[索引从零到n]

string code= "\0\u0001\0\0\0????\u0001\0\0\0\0\0\0\0\u000f\u0001\0\0\0\u001f\u0001\\ABC01\0\0\0\u001f\0\0\0\u0002\DEF\01\0\0\0\u001f\0\0\0\u0003\\\GHI01\0\0\0\u001f\0\0\0";

GetUpperLetters(code)[0] returns ABC
GetUpperLetters(code)[1] returns DEF
GetUpperLetters(code)[2] returns GHI

非常感谢大家的建议答案。 @Immersive的一位成功了! 要做的更改是字符串代码= @“ \\ 0 \\ u0001 \\ 0 \\ 0 \\ 0 ???? \\ u0001 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ u000f \\ u0001 \\ 0 \\ 0 \\ 0 \\ u001f \\ U0001 \\ ABC01 \\ 0 \\ 0 \\ 0 \\ u001f \\ 0 \\ 0 \\ 0 \\ U0002 \\ DEF \\ 01 \\ 0 \\ 0 \\ 0 \\ u001f \\ 0 \\ 0 \\ 0 \\ U0003 \\\\ GHI01 \\ 0 \\ 0 \\ 0 \\ u001f \\ 0 \\ 0 \\ 0“和

code.Substring((code.IndexOf(@“ \\ u000” + i)+(@“ \\ u000” + i).Length),code.IndexOf(@“ \\ u000” +(i + 1))-代码。 IndexOf(@“ \\ u000” + i)-(@“ \\ u000” + i).Length));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM