如何将字符串分成字母组?

时间:2019-09-12 00:44:56

标签: c#

对于我的C#类,我的讲师正在让我们提示用户输入一个开始和结束输入,以将单词拆分到不同的位置。

我的主要问题是我不知道要声明什么变量以及如何将其拆分。

string reply = "";
string userword = "";
string splitword = "";
int length = 0;
int startingindex = 0; 

System.Console.WriteLine("Enter a single world: ");
userword = System.Console.ReadLine();

System.Console.WriteLine("Enter a starting index: ");
startingindex = Convert.ToInt32( System.Console.ReadLine());
// ?
System.Console.WriteLine("Enter a length: ");
length = Convert.ToInt32(System.Console.ReadLine()); 
// ?

例如,如果输入的单词是“ tomato”,并且用户输入的起始索引为2且长度为3,则返回语句将为“ mat”。

4 个答案:

答案 0 :(得分:3)

Console.WriteLine(userword.Substring(startingindex,length));

答案 1 :(得分:0)


            String userword = "";
            String splitword = "";
            int length = 0;
            int startingindex = 0; 

            System.Console.WriteLine("Enter a single world: ");
            userword = System.Console.ReadLine();

            System.Console.WriteLine("Enter a starting index: ");
            startingindex = Convert.ToInt32( System.Console.ReadLine());
            System.Console.WriteLine("Enter a length: ");
            length = Convert.ToInt32(System.Console.ReadLine()); 

            splitword = userword.Substring(startingindex, length);
            System.Console.WriteLine(splitword);

答案 2 :(得分:0)

这样可以吗?

    string reply = "";
    string userword = "";
    string splitword = "";
    int length = 0;
    int startingindex = 0; 

    System.Console.WriteLine("Enter a single world: ");
    userword = System.Console.ReadLine();

    System.Console.WriteLine("Enter a starting index: ");
    startingindex = Convert.ToInt32( System.Console.ReadLine());  

    System.Console.WriteLine("Enter a length: ");
    length = Convert.ToInt32(System.Console.ReadLine()); 

    splitword = userword.Substring(startingindex, length);
    System.Console.WriteLine(splitword);

答案 3 :(得分:0)

string reply = "";
string userword = "";
string splitword = "";
int length = 0;
int startingindex = 0; 

System.Console.WriteLine("Enter a single world: ");
userword = System.Console.ReadLine();

System.Console.WriteLine("Enter a starting index: ");
startingindex = Convert.ToInt32( System.Console.ReadLine());

System.Console.WriteLine("Enter a length: ");
length = Convert.ToInt32(System.Console.ReadLine()); 

System.Console.WriteLine(useword.Substring(startingindex, length);