问题描述
我想查看输入的字符串长度。但是找不到。这里是代码
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间ConsoleApplication2
{
class Program
{
static void Main(string [] args)
{
int i,元音,consonent,digit,space,others,lenght;
i = vowel = consonent = digit = space = others = 0;
string [] array1 = new string [25];
Console.WriteLine(输入你想要计算的句子。但不能超过25个字符:\ n);
for(int j = 0; j< array1.Length; j ++)
{
array1 [j] = Console.ReadLine();
// Console.WriteLine(Enter);
}
lenght = array1.Length;
Console.WriteLine(lenght);
}
}
}
i want to see string length of a input. but can't find out. here is code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i,vowel, consonent, digit, space, others, lenght;
i=vowel=consonent=digit=space=others=0;
string[] array1 = new string[25];
Console.WriteLine("Enter Your Sentence That U Want To Calculate.\nBut The Sentence Not More Than 25 Character:\n");
for (int j = 0; j < array1.Length; j++)
{
array1[j] = Console.ReadLine();
// Console.WriteLine("Enter");
}
lenght = array1.Length;
Console.WriteLine(lenght);
}
}
}
推荐答案
int maxLength = 25;
int digit = 0;
var theString = Console.ReadLine();
//If size is > 25 characters, limit to 25
theString = theString.Substring(0, theString.Length > maxLength ?
maxLength : theString.Length);
//Iterate over all characters in string, checking, if it is digit, space or whatever.
foreach (var ch in theString)
{
if (char.IsDigit(ch))
{
digit++;
}
//Similary for other types of char
}
Console.WriteLine(theString.Length);
string userInput = Console.ReadLine();
Console.WriteLine(String.Format("Your sentence is {0} characters long", userInput.Length));
祝你好运!
Eduard
Good luck!
Eduard
static void Main(string[] args)
{
int i, vowel, consonent, digit, space, others, lenght;
i = vowel = consonent = digit = space = others = 0;
char[] array1 = new char[25];
Console.WriteLine("Enter Your Sentence That U Want To Calculate.\nBut The Sentence Not More Than 25 Character:\n");
for (int j = 0; j < array1.Length; j++)
{
array1[j] = Console.ReadKey(false).KeyChar;
}
lenght = array1.Length;
Console.WriteLine(lenght);
Console.ReadKey(true);
}
这篇关于我想看一个输入的字符串长度。但是找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!