简体   繁体   English

C#String.Substring剪切出一个IpAddress和一个字符串

[英]C# String.Substring cut out a IpAddress and a String

The following code allways crashes. 以下代码始终崩溃。 The input is like this: String 1 is a IpAddress ( 192.168.187.815 ) string 2 should be a Port so the whole input is 192.168.187.815,2332 . 输入是这样的: String 1IpAddress192.168.187.815string 2应该是Port,所以整个输入是192.168.187.815,2332 The code wich should cut out the ipAddress ( string 1 ) and the port ( string 2 ) looks like this: 该代码应剪切出ipAddress( string 1 ),端口( string 2 )如下所示:

Console.Write("String1,String2>");
string both = Console.ReadLine();
string string1 = both.Substring(0,both.IndexOf(","));
Console.WriteLine(string1.Length);
string string2 = both.Substring( string1.Length , both.Length - 1);
Console.WriteLine(string1);
Console.WriteLine(string2);
Console.Read();      

does anyone knows why it isn't working? 有谁知道为什么它不起作用? I was asked for the exception so here it is. 我被要求提供例外,所以就在这里。 There is one little problem i am from germany and the exception is in german. 我来自德国,这是一个小问题,但德语例外。 i tried to translate it with google translater but i think we all know its crap. 我试图用Google Translator进行翻译,但我想我们都知道它的废话。 well here is the original german exception Der Index und die Länge müssen sich auf eine Position in der Zeichenfolge beziehen. Parametername: length 这就是原始的德国例外,即Der Index und die Länge müssen sich auf eine Position in der Zeichenfolge beziehen. Parametername: length Der Index und die Länge müssen sich auf eine Position in der Zeichenfolge beziehen. Parametername: length and here is my bad translation : the index and the length should be specified on a position in the string . parameter : Length Der Index und die Länge müssen sich auf eine Position in der Zeichenfolge beziehen. Parametername: length ,这是我不好的翻译: the index and the length should be specified on a position in the string . parameter : Length the index and the length should be specified on a position in the string . parameter : Length i hope this make sense the index and the length should be specified on a position in the string . parameter : Length我希望这有意义

the second parameter of String.Substring denotes the length of the substring or the number of characters that you want to pull from the string. String.Substring的第二个参数表示子字符串的长度或您要从字符串中提取的字符数。

length 长度
Type: System.Int32 类型:System.Int32
The number of characters in the substring. 子字符串中的字符数。

Your solution would be to subtract the start index from the both.Length . 您的解决方案是从both.Length减去起始索引。 This way you get the number of characters between the , and the end of the entire string both 这样,你得到的之间的字符数,并且整个字符串的结尾both

string string2 = both.Substring(string1.Length+1, both.Length - 1-string1.Length);

Also note that you should take the start index +1 if you want to avoid the , in your string/portnumber. 另请注意,如果要避免在字符串/端口号中使用,则应采用起始索引+1。

Another possibility would be to use the String.Split method to split at the separator , . 另一种可能性是使用String.Split方法在分离器分裂, It will return an string[] and you can easily assign the values: 它将返回一个string[] ,您可以轻松分配值:

string both = "192.168.187.815,2332";
string[] splitted = both.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
string string1 = splitted[0];
string string2 = splitted[1];


Console.WriteLine(string1);
Console.WriteLine(string2);

Disclaimer: this solution implies strongly that you really have an input string in the format "something,someting" otherwise you might run into an index out of bounds. 免责声明:此解决方案强烈暗示您确实有一个格式为"something,someting"的输入字符串,否则可能会遇到超出范围的索引。 You should check the length of the array splitted before accessing hard coded indexes! 在访问硬编码索引之前,您应该检查splitted后的数组的长度!

It's clear that the separator is , then why are you complicating things by using Substring why not proceed with Split like the following: 显然,分隔符为,那么为什么要使用Substring使事情复杂化,为什么不像下面这样继续进行Split

    string[] inputParams= both.Split(',');
    string ipAddress = inputParams[0];
    string portNumber =inputParams[1];
  • Actually the second parameter of the String.Substring() method represents the number of characters(not the last index as you thought). 实际上, String.Substring()方法的第二个参数表示字符数(而不是您想到的最后一个索引)。
  • If you again wish to proceed with Substring the you can try like the following: 如果您再次希望继续使用Substring则可以尝试以下操作:

     string ipAddress = both.Substring(0,both.IndexOf(",")); string portNumber =both.Substring(both.IndexOf(",")+1) 

    But still the first one is the best for this situation 但第一个仍然是这种情况的最佳选择

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

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