使用子字符串的问题 - C# [英] Problem with using substring - C#

查看:85
本文介绍了使用子字符串的问题 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中遇到一个小问题:这个代码:

 string msg = words [cp] .Substring(words [cp] .IndexOf('=' )+ 2,单词[cp] .Length  -  1); 



我真的需要修复它,因为我正在尝试编写一种编程语言。



这是例外:

 System.ArgumentOutOfRangeException发生
HResult = 0x80131502
消息=索引和长度必须引用字符串中的位置。
参数名称:length
Source = mscorlib
StackTrace:
at System.String.Substring(Int32 startIndex,Int32 length)
at ConsoleApp1.Program.Main(String [] args)在C:\ Users \ * \source \repos\WindowsFormsApp1 \ConsoleApp1 \Program.cs:第30行





这是我的代码:

 Lexer lex = new Lexer(); 

string code =def string s = hello world \\\
read key;
int cp = 0;
int ts = 0;
lex.AddKeyword(def);
lex.AddKeyword(string);
lex.AddKeyword(read);
lex.AddKeyword(key);
string [] words = code.Split('\ n');
string [] identifers = new string [] {};
if(words [cp] .Substring(0,3)==def)
{
if(words [cp] .Substring(4,6)==string )
{
string msg = words [cp] .Substring(words [cp] .IndexOf('=')+ 2,words [cp] .Length - 1);
if(!words [cp] .Contains('=')|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(。) || words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(+)|| words [cp] .Substring(12,msg.Length - 1 - msg .Length - 1 - 4).Contains(=)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains( - )|| words [ cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(()|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains())|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(*)|| words [cp] .Substring (12,msg.Length - 1 - msg.Length - 1 - 4).Contains(&)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4) .Contains(^)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(%)|| words [cp] .Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains($)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains( #)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(@)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(!)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(〜)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(`)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(1)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(2)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(3)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(4)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(5)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(6)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(7)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(8)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(9)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(0)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(;)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(:)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(\\)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(/)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(\)|| words [cp] .Substring(12,msg.Length - 1 - msg .Length - 1 - 4).Contains(')|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(。)|| words [ cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(,)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains({)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(})|| words [cp] .Substring (12,msg.Length - 1 - msg.Length - 1 - 4).Contains([)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4)。包含(])|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(|))
{
Console。 WriteLine(语法错误:\ nat:\ n+ + cp +\ n -------------按任意键继续----------- - );
Console.ReadKey();
}
Console.Wri teLine(msg);
identifers [cp] = words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4);
cp ++;
}
else if(words [cp] .StartsWith(read))
{
if(words [cp] .Substring(6)==key)
{
Console.WriteLine(-------------按任意键继续-------------);
Console.ReadKey();
}
}
}





注意:

Lexer是一个仅为此目的而创建的自定义类,它只有一些简单的List< string>在它和添加到它们的方法。



我正在使用Visual Studio 2017 .NET Framework 4.6(此框架附带包)



我尝试了什么:



我已经尝试了所有可以找到的东西,没有任何效果。

解决方案

)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(# )|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(@)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(!)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(〜)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(`)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(1)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(2)|| words [cp]。子串(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(3)|| words [cp] .Substring(12,msg.Length - 1 - msg .Length - 1 - 4).StartsWith(4)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(5)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(6)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(7)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(8)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(9)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).StartsWith(0)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(;)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(:)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(\\)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(/)||单词[cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(\)|| words [cp] .Substring(12,msg.Length - 1 - msg .Length - 1 - 4).Contains(')|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(。)|| words [ cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(,)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains({)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(})|| words [cp] .Substring (12,msg.Length - 1 - msg.Length - 1 - 4).Contains([)|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4)。包含(])|| words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4).Contains(|))
{
Console。 WriteLine(语法错误:\ nat:\ n+ + cp +\ n -------------按任意键继续----------- - );
Console.ReadKey();
}
Console.Wri teLine(msg);
identifers [cp] = words [cp] .Substring(12,msg.Length - 1 - msg.Length - 1 - 4);
cp ++;
}
else if(words [cp] .StartsWith(read))
{
if(words [cp] .Substring(6)==key)
{
Console.WriteLine(-------------按任意键继续-------------);
Console.ReadKey();
}
}
}





注意:

Lexer是一个仅为此目的而创建的自定义类,它只有一些简单的List< string>在它和添加到它们的方法。



我正在使用Visual Studio 2017 .NET Framework 4.6(此框架附带包)



我尝试了什么:



我已经尝试了所有可以找到的东西,没有任何效果。


数组 [ ^ ]是一个基于零的索引,你将考虑从0

开始的索引,以避免 ArgumentOutOfRangeException [ ^ ]您必须在访问指定索引的项目之前验证集合的索引,该索引是causin g例外。



验证为



 string msg =; 
if(words.Length> cp)
{
string word = words [cp];
var start = word.IndexOf('=')+ 2;
var length = word.Length - 1;
if(word.Length> start)
msg = word.Substring(start,length);
}


使用调试器,您可以轻松找到这个。在该行上设置断点并运行代码。当代码中断时,将鼠标悬停在单词[cp]变量上以查看其内容。你可以在那里找到它。



在字符串中找不到'='符号和/或字符串的长度不是您认为它是什么。



在任何情况下,调试器都可以调试您,特别是您在处理数据的上下文中对代码的理解。 / BLOCKQUOTE>

I've had a small problem in C# with this code:

string msg = words[cp].Substring(words[cp].IndexOf('=') + 2, words[cp].Length - 1);


I really need this to be fixed, because I'm trying to make a programming language.

Here's the exception:

System.ArgumentOutOfRangeException occurred
  HResult=0x80131502
  Message=Index and length must refer to a location within the string.
Parameter name: length
  Source=mscorlib
  StackTrace:
   at System.String.Substring(Int32 startIndex, Int32 length)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\*\source\repos\WindowsFormsApp1\ConsoleApp1\Program.cs:line 30



Here's my code:

Lexer lex = new Lexer();

            string code = "def string s = hello world\nread key";
            int cp = 0;
            int ts = 0;
            lex.AddKeyword("def");
            lex.AddKeyword("string");
            lex.AddKeyword("read");
            lex.AddKeyword("key");
            string[] words = code.Split('\n');
            string[] identifers = new string[] { };
            if (words[cp].Substring(0, 3) == "def")
            {
                if (words[cp].Substring(4, 6) == "string")
                {
                    string msg = words[cp].Substring(words[cp].IndexOf('=') + 2, words[cp].Length - 1);
                    if (!words[cp].Contains('=') || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(".") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("+") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("=") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("-") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("(") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(")") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("*") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("&") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("^") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("%") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("$") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("#") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("@") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("!") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("~") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("`") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("1") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("2") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("3") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("4") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("5") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("6") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("7") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("8") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("9") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("0") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(";") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(":") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("\\") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("/") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("\"") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("'") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(".") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(",") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("{") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("}") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("[") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("]") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("|"))
                    {
                        Console.WriteLine("Syntax error: \nat: \n  " + cp + "\n-------------Press any key to continue-------------");
                        Console.ReadKey();
                    }
                    Console.WriteLine(msg);
                    identifers[cp] = words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4);
                    cp++;
                }
                else if (words[cp].StartsWith("read "))
                {
                    if (words[cp].Substring(6) == "key")
                    {
                        Console.WriteLine("-------------Press any key to continue-------------");
                        Console.ReadKey();
                    }
                }
            }



NOTE:
Lexer is a custom class created for this purpose only, it just has some simple List<string> in it and methods to add to them.

I'm using Visual Studio 2017 .NET Framework 4.6 (this framework came with the package)

What I have tried:

I've tried everything I can find, nothing works.

解决方案

") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("#") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("@") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("!") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("~") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("`") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("1") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("2") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("3") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("4") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("5") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("6") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("7") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("8") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("9") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).StartsWith("0") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(";") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(":") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("\\") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("/") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("\"") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("'") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(".") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains(",") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("{") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("}") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("[") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("]") || words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4).Contains("|")) { Console.WriteLine("Syntax error: \nat: \n " + cp + "\n-------------Press any key to continue-------------"); Console.ReadKey(); } Console.WriteLine(msg); identifers[cp] = words[cp].Substring(12, msg.Length - 1 - msg.Length - 1 - 4); cp++; } else if (words[cp].StartsWith("read ")) { if (words[cp].Substring(6) == "key") { Console.WriteLine("-------------Press any key to continue-------------"); Console.ReadKey(); } } }



NOTE:
Lexer is a custom class created for this purpose only, it just has some simple List<string> in it and methods to add to them.

I'm using Visual Studio 2017 .NET Framework 4.6 (this framework came with the package)

What I have tried:

I've tried everything I can find, nothing works.


since Array [^] is a Zero based index you will have consider the index starting from 0
in order to avoid ArgumentOutOfRangeException[^] you will have to validate the index of the collection before accessing the Item of a specified index which is causing the exception.

Validate as

string msg = "";
           if (words.Length > cp)
           {
               string word = words[cp];
               var start = word.IndexOf('=') + 2;
               var length = word.Length - 1;
               if (word.Length > start)
                   msg = word.Substring(start, length);
           }


Using the debugger, you can easily figure this one out. set a breakpoint on that line and run the code. When the code breaks, hover the mouse over the words[cp] variable to see it's content. You can pretty much figure it out there.

Either the '=' sign isn't being found in the string and/or the length of the string isn't what you think it is.

In any case, the debugger is there to debug YOU, specifically your understanding of the code in the context of the data it's processing.


这篇关于使用子字符串的问题 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆