C# 不使用内置函数计算单词长度

C# 不使用内置函数计算单词长度,c#,string,C#,String,这是我遇到并失败的一个问题 假设说 string str = "wordcounter"; 使用str.Length 但是,在C语言中,是否可以在不使用任何内置函数(如Length,SubStr等)的情况下获取字母数 int myOwnGetStringLength(String str) { int count = 0; foreach(Char c in str) count++; return count; } 您可以写入一个循环并在此循环内递增

这是我遇到并失败的一个问题

假设说

string str = "wordcounter";
使用
str.Length

但是,在C语言中,是否可以在不使用任何内置函数(如
Length
SubStr
等)的情况下获取字母数

int myOwnGetStringLength(String str)
{
    int count = 0;
    foreach(Char c in str)
        count++;
    return count;
}

您可以写入一个循环并在此循环内递增一个计数器:

int numberOfLetters = 0;
foreach (var c in str)
{
    numberOfLetters++;
}
// at this stage numberOfLetters will contain the number of letters 
// that the string contains
还有另一种方法:

int numberOfLetters = str.ToCharArray().Length;
还有另一种甚至更疯狂的方式,使用的是在一台计算机上运行的。NET中的字符串通过使用一个4字节整数在内存中进行布局,该整数包含字符串的长度,后跟表示每个字符的多个2字节UTF-16字符。这类似于。因此:


显然,在任何实际的生产代码中都不应该使用内置的
Length
函数,而应该这样做,这里显示的代码不应该被认真对待。

不是很快,但您可以始终循环并计算包含的字符数

int counter = 0;
foreach (var caracter in str)
{
    counter ++;
}

我的回答有点晚了,但我想发布同样的内容。虽然上面提到的所有解决方案都是正确的,但我相信foreach的IL在迭代之前确实知道iterable的长度。说到纯解决方案,这是我的:

    private int stringLength(string str) {
        int length = 0;
        bool done = false;
        do {
            try {
                char c = str[length];
                length++;
            } catch (IndexOutOfRangeException) {
                done = true;
            }
        } while(!done);
        return length;
    }

我的一般解决方案是不使用“foreach”或“StringBuilder”(这是C#特有的),也不捕获任何异常

            string str = "wordcounter";
            str += '\0';
            int x = 0;
            while (str[x] != '\0')
                x++;
            Console.WriteLine(x);     //Outputs 11

为什么不想使用内置函数呢?我尝试了
string temp=“Hello World”;int i=0;对于(i=0;temp[i]!='\0';i++);控制台写入线(i)
并失望地发现它不包含空终止字符。我把我的尝试写在这里,以备将来参考。当我无法理解有内置属性时,为什么会有人想手动执行此操作。我甚至不确定你和我提出的这个方法是否适用于所有Unicode字符。我会坚持使用String.Length
int numberOfLetters=str.ToCharArray().Length但在这行代码中,我们仍然有内置函数。与以前的答案相比,这个答案中是否有新的信息?
namespace ConsoleApplication {  
class Program {  
    static void Main(string[] args) {  
        string testString = "testing";  
        int x = 0;  
        foreach(char c in testString) {  
            x++;  
        }  
        Console.WriteLine("\nLength Of String:{0}", (x));  
        Console.Read();  
    }  
}  
class Program
{
    static void Main(string[] args)
    {

        string Name = "He is palying in a ground.";
        char[] characters = Name.ToCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = Name.Length - 1; i >= 0; --i)
        {
            sb.Append(characters[i]);
        }
        Console.Write(sb.ToString());
        Console.Read();

    }
}
class Program
 {
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a string to find its lenght");
        string ch = Console.ReadLine();
        Program p = new Program();
        int answer = p.run(ch);
        Console.WriteLine(answer);
        Console.ReadKey();
    }
    public int run(string ch)
    {
        int count = 0;
        foreach (char c in ch)
        {
            count++;
        }
        return count;
    }
}
            string str = "wordcounter";
            str += '\0';
            int x = 0;
            while (str[x] != '\0')
                x++;
            Console.WriteLine(x);     //Outputs 11
class Program
{
    static void Main(string[] args)
    {
        string test = "test";

        //string as char array:
        //iterate through char collection
        foreach (char c in test)
        {
           //do something
        }
        //access elements by index
        Console.WriteLine("Contents of char array : {0}, {1}, {2}, {3}", test[0], test[1], test[2], test[3]);
        Console.ReadKey();
    }
}