如何测试数组中的所有字符串是否具有相同的长度

发布于 2025-02-13 00:42:08 字数 466 浏览 2 评论 0原文

我有一系列的字符串。
如何测试此数组中的所有元素是否具有相同的长度?

这是我到目前为止所拥有的:

public static bool ComparingStrings(string[] words)
{
    bool result = false;
    for (int i = 0; i < words.Length; i++)
    {
        string s = words[i];
        if (s.Length == words.Length)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }

    return result;
}

I have an array of strings.
How to test if all elements in this array have the same length ?

Here is what I have so far :

public static bool ComparingStrings(string[] words)
{
    bool result = false;
    for (int i = 0; i < words.Length; i++)
    {
        string s = words[i];
        if (s.Length == words.Length)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }

    return result;
}

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

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

发布评论

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

评论(6

煮茶煮酒煮时光 2025-02-20 00:42:09
Words.All(s=>s.Length==Words.Length)

使用检查所有字符串的长度与第一个字符串相同。午夜不适用于空数组

Words.All(s=>s.Length==Words.Length)

Use check if all strings have same length as first one. Nite will not work on empty array

枫以 2025-02-20 00:42:08

您发布的代码将比较每个字符串的长度(以字符为单位)与整体数组的长度(元素数),因为myArray.length.length提供了数组中的元素数量,和“ String” .length给出字符的数量。您必须执行的过程是记录第一个字符串的长度,并将该长度与数组中的每个字符串进行比较,以查看是否有任何不相同的长度。

在C#中:

string[] myArray = new String[] {"four", "char", "word"};
boolean allSameLength = true;
int firstLen = myArray[0].Length;

for (int i = 1; i < myArray.Length; i++)
{
   if (myArray[i].Length != firstLen)
   {
      allSameLength = false;
      break;
   }
}

此功能封装在PM100的响应中。 将确定数组中的元素是否通过逻辑测试,而 lambda operator测试要应用(s.length ==单词[0] .length)。

The code you have posted will compare the length of each string (in characters) to the length of the overall array (in number of elements), since myArray.Length givees the number of elements in an array, and "string".Length gives the number of characters. The process that you have to do is record the length of the first string, and compare that length to every string in the array to see if there are any which are not the same length.

In C#:

string[] myArray = new String[] {"four", "char", "word"};
boolean allSameLength = true;
int firstLen = myArray[0].Length;

for (int i = 1; i < myArray.Length; i++)
{
   if (myArray[i].Length != firstLen)
   {
      allSameLength = false;
      break;
   }
}

This functionality is encapsulated in pm100's response. .All() will determine whether ever element in an array passes a logical test, and the lambda operator is used to pass the logical test to apply (s.length==Words[0].Length).

断爱 2025-02-20 00:42:08

使用linq非常简单:

public bool CompareStringLength(string[] words)
{
    return words.Select(w => w.Length).Distinct().Count() == 1;
}

一个更高效的版本(遇到两个不同的长度时停止):

public bool CompareStringLength(string[] words)
{
    return words.Select(w => w.Length)
                .Distinct()
                .Take(2)
                .Count() == 1;
}

nota:问题中未提供数组为空时的预期结果。
如果您需要true在这种情况下,请使用count()&lt; = 1;

Using Linq it's pretty straightforward:

public bool CompareStringLength(string[] words)
{
    return words.Select(w => w.Length).Distinct().Count() == 1;
}

A more efficient version (that stop when two distinct length are encountered):

public bool CompareStringLength(string[] words)
{
    return words.Select(w => w.Length)
                .Distinct()
                .Take(2)
                .Count() == 1;
}

Nota: The expected result when the array is empty is not provided in the question.
If you need true in this case, use Count() <= 1;

凉薄对峙 2025-02-20 00:42:08

简单方法:

    public static bool CompareSetrings(string[] words)
    {
        // Return false is the array is empty
        if(words.Length == 0)
            return false;

        // The var will hold the length  of the first string
        int initialLength = words[0].Length;

        // Iterating trough string array
        for(int i = 0; i < words.Length; i++)
        {
            // If the length is different, return false
            if (initialLength != words[i].Length)
                return false; 
        }
        return true;
    }

Simple method:

    public static bool CompareSetrings(string[] words)
    {
        // Return false is the array is empty
        if(words.Length == 0)
            return false;

        // The var will hold the length  of the first string
        int initialLength = words[0].Length;

        // Iterating trough string array
        for(int i = 0; i < words.Length; i++)
        {
            // If the length is different, return false
            if (initialLength != words[i].Length)
                return false; 
        }
        return true;
    }
橪书 2025-02-20 00:42:08

尝试一下

public static bool CompareSetrings(string[] words)
{
    var len = words[0].Length;
    foreach (string word in words)
    if(word.Length != len) return false;
    return true;
}

try this

public static bool CompareSetrings(string[] words)
{
    var len = words[0].Length;
    foreach (string word in words)
    if(word.Length != len) return false;
    return true;
}
错々过的事 2025-02-20 00:42:08

功能接受字符串数组。如果数组为null,或者数组中只有0或1个字,它将返回true。如果有多个单词,它会抓住数组中的第一个单词的长度,然后使用Linq的所有方法来检查数组中的所有字符串是否具有将每个单词的长度与每个单词的长度与第一个单词的长度(firstWordLength)。它跳过(跳过(1))数组中的第一个单词,因为我们使用了第一个单词的长度,因此我们删除了冗余检查。

public static bool CheckAllStringLengthsAreEqual(string[] words)
{
    if (words is null || words.Length <= 1)
    {
        return true;
    }

    var firstWordLength = words[0].Length;

    return words.Skip(1).All(x => x.Length == firstWordLength);
}

Function accepts a string array. It will return true if the array is null or there is only 0 or 1 word in the array. If there is more than one word, it grabs the length of the first word in the array then uses LINQ's All method to check if all the strings in the array have the same length by comparing each word's length to the length of the first word (firstWordLength). It skips (Skip(1)) the first word in the array because we use the first word's length so we remove a redundant check.

public static bool CheckAllStringLengthsAreEqual(string[] words)
{
    if (words is null || words.Length <= 1)
    {
        return true;
    }

    var firstWordLength = words[0].Length;

    return words.Skip(1).All(x => x.Length == firstWordLength);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文