【问题标题】:Skipping a range of values in for loop C#在 for 循环 C# 中跳过一系列值
【发布时间】:2020-06-22 01:14:44
【问题描述】:

我正在尝试循环遍历字符串中的字符。

string cycleMe = "Hi StackOverflow! Here is my string."

但是,我想跳过某些索引范围。我想跳过的范围存储在对象列表中,delims。

List<Delim> delims = delimCreator();

要检索范围的每个起始索引和结束索引,我必须编写一个访问每个“delim”的循环:

delims[0].getFirstIndex() //results in, say, index 2
delims[0].getLastIndex() //results in, say, index 4

delims[1].getFirstIndex() //results in, say, index 5
delims[1].getLastIndex() //results in, say, index 7

(可以有无限多个“delim”对象)

如果以上是我的列表,我想打印字符串 cycleMe,但跳过 2 和 4(含)和 5 和 7(含)之间的所有字符。

使用上述数字的预期输出:

HiOverflow! Here is my string.

这是我到目前为止编写的代码。它循环的频率比我预期的要高得多(它循环〜x2字符串中的字符数)。提前致谢! =)

  List<Delim> delims = delimAggregateInator(displayTextRaw);

  for (int x = 0; x < cycleMe.Length;x++){

    for (int i = 0; i < delims.Count; i++){

      if (!(x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex())){


          Debug.Log("test");


      }
    }

【问题讨论】:

  • “跳过”是什么意思?字符串“abcdefgh”是否应该被修剪为“abcfgh”之类的内容,或者您​​希望使用空格代替“abc fgh”?
  • @JustShadow 要么/要么是好的。
  • 要澄清这个问题,请添加您的输入、预期输出和代码的输出。展示您想要实现但失败的目标。
  • @greenorangesan,我在下面更新了我的答案。请看一看。
  • 您是 Java 开发人员吗?当 C# 中的惯用代码是 .FirstIndex 时,.getFirstIndex() 是什么?

标签: c# .net loops object logic


【解决方案1】:

解决方案可能是将字符串转换为 char 数组,将所需部分替换为空格,然后将输出转换回字符串。

这是您的代码的修改版本:

string cycleMe = "Hi StackOverflow! Here is my string."
var charArray = cycleMe.ToCharArray(); // Converting to char array

List<Delim> delims = delimAggregateInator(displayTextRaw);
for (int x = 0; x < cycleMe.Length;x++){
  for (int i = 0; i < delims.Count; i++){
    // ORIGINAL: if (!(x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex())){
    if (x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex()){
       Debug.Log("test");
       charArray[x] = ' '; // Replacing the item with space
    }
}

string output = new string(charArray); // Converting back to string

附:这可能不是最理想的解决方案,但至少它应该可以工作。

【讨论】:

    【解决方案2】:

    我认为跳过意味着您想从原始字符串中省略这些字符。如果是这种情况,您可以尝试如下聚合扩展方法。

    string result = delims.Aggregate<Delim, string>(cycleMe, (str, d) => cycleMe = cycleMe.Remove(d.FirstIndex, (d.LastIndex - d.FirstIndex) + 1));
    

    确保分隔符列表的顺序正确。

    【讨论】:

      【解决方案3】:

      你应该为此使用 LINQ

              struct Delim
              {
                  public int First { get; set; }
                  public int Last { get; set; }
              }
      
              static void Main(string[] args)
              {
                  string cycleMe = "Hi StackOverflow! Here is my string.";
      
                  var delimns = new List<Delim> { new Delim { First=2, Last=4}, new Delim { First = 5, Last = 7 } };
      
                  var cut = cycleMe.Where((c, i) => 
                     !delimns.Any(d => i >= d.First && i <= d.Last));
      
                  Console.WriteLine(new string(cut.ToArray());
              }
      

      这意味着我基本上只在不属于任何切割范围的位置选择字母。

      另外:修正你的命名。分隔符是一个字符,而不是一个位置(数字)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        • 2016-07-12
        • 2022-01-12
        相关资源
        最近更新 更多