我想看一个输入的字符串长度。但是找不到 [英] i want to see string length of a input. but can't find out

查看:42
本文介绍了我想看一个输入的字符串长度。但是找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看输入的字符串长度。但是找不到。这里是代码

使用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);
}
}
}

推荐答案

我不确定,如果我理解你的问题。从您的代码中,您将要求用户输入长度小于25个字符的1个句子。但是你要求25个任意长度的句子。



此代码请求用户输入,然后输入限制为25个字符。在它之后你可以检查输入字符串中的字符类型。

I'm not sure, if I understood your question correctly. From your code, you're going to ask user to enter 1 sentence with length lesser then 25 characters. But you're asking for 25 sentences of arbitrarily length.

This code requests for user input, then input is limited to 25 characters. After it you can check, what types of characters is in input string.
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);


为什么不:

why not :
string userInput = Console.ReadLine();
Console.WriteLine(String.Format("Your sentence is {0} characters long", userInput.Length));





祝你好运!

Eduard



Good luck!
Eduard


您好试试这个代码...







Hi Try this code...



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);
        }


这篇关于我想看一个输入的字符串长度。但是找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆