[英]How to split a string with a certain character without considering the length of the character?
[英]How to read characters of string or character array without using length function
我想从字符串中读取所有字符而不使用内置函数。我尝试了以下方法。
char[] str ={ 'k','r','i','s'};
for ( int i = 0; str[i]!='\0'; i++)
{
Console.WriteLine(str[i]);
}
但是我得到一个例外,因为与c不同,我看不到这里的字符串以空字符结尾。 还有其他方法(除了在异常情况下使用内置函数/ try catch块之外),我可以读取字符串的所有字符吗?
在c#中,数组具有Length
属性:
char[] str ={ 'k','r','i','s'};
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
否则,您可以使用foreach
来枚举数组中的所有字符。
如果您真正想要显示的是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.