简体   繁体   English

如何在不使用长度函数的情况下读取字符串或字符数组的字符

[英]How to read characters of string or character array without using length function

I want to read all the characters from the string without using the built in functions.I tried the below. 我想从字符串中读取所有字符而不使用内置函数。我尝试了以下方法。

        char[] str ={ 'k','r','i','s'};            
        for ( int i = 0; str[i]!='\0'; i++)
        {
            Console.WriteLine(str[i]);
        }

But I get an exception because unlike in c I don't see the string here ends with a null character. 但是我得到一个例外,因为与c不同,我看不到这里的字符串以空字符结尾。 Is there any other way (apart from using built functions/try catch block in case of exception) I can read all the characters of the string ? 还有其他方法(除了在异常情况下使用内置函数/ try catch块之外),我可以读取字符串的所有字符吗?

In c# arrays have Length property: 在c#中,数组具有Length属性:

char[] str ={ 'k','r','i','s'};            
for (int i = 0; i < str.Length; i++)
{
    Console.WriteLine(str[i]);
}

Otherwise you can use foreach which will enumerate all characters in an array. 否则,您可以使用foreach来枚举数组中的所有字符。

If what you really want is to display the string that the characters in the char array produce: 如果您真正想要显示的是char数组中的字符产生的字符串:

char[] mychars = { 'k', 'r', 'i', 's' };
Console.WriteLine("Your characters form the following string: " + new string(mychars));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不考虑字符长度的情况下拆分具有特定字符的字符串? - How to split a string with a certain character without considering the length of the character? 如何在不中断单词的情况下按字符和行长中断字符串? - How to break string by character and line length, without breaking words? 使用Func <string, bool> ,如何分割一个字符,然后计算一个函数中这些值的长度 - using Func<string, bool>, how can I split on a character then count the length of those values within a function 从文本框中输入的字符串中读取所有字符,不重复和每个字符的计数 - To read all characters from a string entered in text box without duplicates and count of each character 如何在C#中生成没有重复(字符)的随机长度为26的字符串? - How to generate Random 26 length character string with no duplicates(characters) in c#? 在不使用C#中内置函数的情况下计算字符串长度 - Calculating string length without using inbuilt function in c# 使用等长的字符串数组,使用 LINQ 获取每个字符串在它自己的数组中的第 n 个字符 - With an array of strings of equal length, get the nth character of each string in it's own array using LINQ 在不使用Length属性的情况下找到数组的长度 - Find the length of an array without using the Length property 使用字符串中的字符填充字符数组 - Filling a character array with characters from a string 不使用Split函数将字符串转换为数组 - Convert string to array in without using Split function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM