如何检查数组中的所有字符串是否与长度相同C#

发布于 2025-02-08 18:03:07 字数 127 浏览 3 评论 0原文

例如,

string[] text=new string[] {"string1", "string2", "string3"};

我怎么知道此数组中的所有字符串长度是否相等?

for instance,

string[] text=new string[] {"string1", "string2", "string3"};

how do i know if all string's length in this array are equal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光暖心i 2025-02-15 18:03:07

另一个解决方案:

bool allSameLength = !text.Any(t => t.Length != text[0].Length));

Another solution:

bool allSameLength = !text.Any(t => t.Length != text[0].Length));
不寐倦长更 2025-02-15 18:03:07

这是一个解决方案,而无需使用(每个)

string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};
int index = 0, firstLength = -1;
bool allSameLength = true;
while(index < text.Length)
{
    int length = (text[index] + '\0').IndexOf('\0');
    firstLength = (index == 0) ? length : firstLength;
    allSameLength &= (length != firstLength) ;
    index += 1;
}

return allSameLength;

Here is a solution without using for(each) as requested:

string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};
int index = 0, firstLength = -1;
bool allSameLength = true;
while(index < text.Length)
{
    int length = (text[index] + '\0').IndexOf('\0');
    firstLength = (index == 0) ? length : firstLength;
    allSameLength &= (length != firstLength) ;
    index += 1;
}

return allSameLength;
ゃ人海孤独症 2025-02-15 18:03:07

这是另一种解决方案,该解决方案不使用(每个),并且不会尝试使用其他类型的循环(例如while)来作弊:

    string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};

    List<string> texts = new List<string>();
    texts.Add(null);
    texts.AddRange(text);
    texts.Add(null);
    
    bool CheckSameLength(int index)
      => (texts[index + 1] == null) ? true
       : texts[index] == null ? CheckSameLength(1)
       : texts[index].Length == texts[index + 1].Length ? CheckSameLength(index + 1)              
       : false;
      
    return CheckSameLength(texts, 0);

Here is another solution that does not use for(each) and does not try to cheat by using a different type of loop (like while):

    string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};

    List<string> texts = new List<string>();
    texts.Add(null);
    texts.AddRange(text);
    texts.Add(null);
    
    bool CheckSameLength(int index)
      => (texts[index + 1] == null) ? true
       : texts[index] == null ? CheckSameLength(1)
       : texts[index].Length == texts[index + 1].Length ? CheckSameLength(index + 1)              
       : false;
      
    return CheckSameLength(texts, 0);
泛滥成性 2025-02-15 18:03:07

最荒谬的递归使用?

public class Program {
  
  public static void Main() {
    string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};
    Console.WriteLine(AllLengthsEqual(text));
  }

  public static bool AllLengthsEqual(string[] strArr) {
    return strArr == null ? true : (strArr.Length < 2 ? true : AllLengthsEqual(strArr, 0));
  }

  private static bool AllLengthsEqual(string[] strArr, int index) {
    return AllLengthsEqual(strArr, index + 1, strArr[index] != null ? strArr[index].Length : -1);
  }

  private static bool AllLengthsEqual(string[] strArr, int index, int prevLength) {    
    if (index < strArr.Length) {
      int thisLength = strArr[index] != null ? strArr[index].Length : -1;
      return (thisLength == prevLength) && AllLengthsEqual(strArr, index + 1, thisLength);
    }
    else 
      return true;    
  }
  
}

Most ridiculous use of recursion?

public class Program {
  
  public static void Main() {
    string[] text = new string[] {"dsasaffasfasfafsa", "siuuuuu", "ewqewqeqeqewqeq"};
    Console.WriteLine(AllLengthsEqual(text));
  }

  public static bool AllLengthsEqual(string[] strArr) {
    return strArr == null ? true : (strArr.Length < 2 ? true : AllLengthsEqual(strArr, 0));
  }

  private static bool AllLengthsEqual(string[] strArr, int index) {
    return AllLengthsEqual(strArr, index + 1, strArr[index] != null ? strArr[index].Length : -1);
  }

  private static bool AllLengthsEqual(string[] strArr, int index, int prevLength) {    
    if (index < strArr.Length) {
      int thisLength = strArr[index] != null ? strArr[index].Length : -1;
      return (thisLength == prevLength) && AllLengthsEqual(strArr, index + 1, thisLength);
    }
    else 
      return true;    
  }
  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文