【问题标题】:I am unable to use substring. How can I fix this?我无法使用子字符串。我怎样才能解决这个问题?
【发布时间】:2020-02-12 15:08:25
【问题描述】:

我正在尝试查看天气字符串是否按字母顺序排列并弹出此错误

System.ArgumentOutOfRangeException:索引和长度必须引用字符串中的位置。 参数名称:长度 在 System.String.Substring(Int32 startIndex,Int32 长度) 在 Rextester.Program.Main(String[] args)**

public static void Main(string[] args)
{
    string str = "bat\ncat\ndog\n";
    int c = 0;
    for (int i = 0; i < str.Length; i++)
    {
        if ((str.Substring(i,i + 1).Equals("\n")))
        {
            c++;
        }

    }
    String[] strArray = new String[c + 1]; //declare with size
    int g = 0;
    String h = "";
    for (int i = 0; i < str.Length; i++)
    {
        if ((str.Substring(i,i + 1).Equals("\n")))
        {
            strArray[g] = h;
            h = "";
            g = g + 1;
        }
        else
        {
            h = h + str.Substring(i,i + 1);
        }
    }
    String p = "True";
    for (int i = 0; i < g; i++)
    {
        if (i < (g - 1))
        {
            String f = strArray[i];
            String g2 = strArray[i + 1];
            char d = f[0];
            char s = g2[0];
            int d1 = (int)d;
            int s1 = (int)s;
            if (d1 > s1)
            {
                p = "False";
            }

        }
    }
    Console.WriteLine(p);
}
}

【问题讨论】:

  • 恕我直言,拆分它,然后检查它们。只需几行代码,效率更高,出错率更低。
  • 您在第一行末尾缺少一个分号。
  • 我修好了。主要问题还是一样
  • 这个问题与 Selenium 有什么关系?
  • 因为我是从 web 元素中获取字符串

标签: c#


【解决方案1】:

不确定您在第二个循环中正在做什么以及为什么它如此复杂。我们可以这样做。希望这会有所帮助。


using System;

public class Program
{
    public static void Main()
    {
    string str = "abcd";
    str = str.Replace('\n',' ');

    String p = "True";
    for (int i = 1; i < str.Length; i++) {  

        // if element at index 'i' is less  
        // than the element at index 'i-1'  
        // then the string is not sorted  
        if (str[i] < str[i - 1]) {
            p = "false";
        } 

    }  
    Console.WriteLine(p);
}
}

【讨论】:

  • 你改变了两个循环吗?
  • 是的,我都改了
  • 在 C# 中,子字符串方法接受两个参数“startIndex”和长度。第一个参数将指定必须检索的子字符串的起始位置,第二个参数将指定子字符串的长度。两个参数的类型都是 System.Int32。所以,你需要用 str.Substring(i,2) 代替。
  • 它已编译但即使字符串不是按字母顺序排列仍然返回true
  • 我只是检查每个单词的第一个字母。例如袋猫狗。这是按字母顺序排列的。 cat bag dog 这不是按字母顺序排列的
【解决方案2】:

注意子串的定义

子字符串从指定的字符位置开始,并有一个 指定长度

考虑到您第一次使用子字符串,这里

 for (int i = 0; i < str.Length; i++)
 {
      if (str.Substring(i, i + 1).Equals("\n"))
      {
                    c++;
      }
 }

当我们到达i=6 时会发生什么? Substring 尝试为您提供一个新字符串,该字符串从位置i = 6 开始,长度为length = 7 个字符。所以它试图给你从 str[6] 到 str[12] 的 7 个字符。好吧,没有 str[12],所以你得到一个异常。

很明显,您的意图不是获取从位置 6 开始且长度为 7 个字符的字符串。你想要一个字符,所以你的循环应该是这样的

for (int i = 0; i < str.Length; i++)
{
    if (str.Substring(i, 1).Equals("\n"))
    {
           c++;
    } 
}

但是有一种更简单的方法可以使用 LINQ 按字母顺序排列单词

 string str = "bat\ncat\ndog\n";

 //Removes the trailing \n so you don't have one entry that is only whitespace
 str = str.Trim(); 

 string[] strSplit = str.Split('\n').OrderBy(x => x[0]).ToArray();

现在所有子字符串都按字母顺序排序,你可以对它们做任何你想做的事情

【讨论】:

  • 这有帮助!但是如果字符串没有 /n 怎么办?
  • @tmk123 这取决于。如果它仍在使用某种分隔符,例如管道 | 或其他东西,那么您只需在 .Split(char c) 调用中更改该字符。否则,它依赖于数据中存在的任何结构来保持条目分开。
猜你喜欢
  • 2013-10-24
  • 1970-01-01
  • 2020-02-21
  • 2019-04-10
  • 1970-01-01
  • 2012-03-16
  • 2020-11-28
  • 2011-10-18
  • 1970-01-01
相关资源
最近更新 更多