替换字符串句子C#中的多个单词

时间:2020-02-14 13:47:37

标签: c# string replace char stringbuilder

大家好,我不需要答案,但我希望知道并找出我做错了什么。作为一个初学者,我在学习中得到了一个非常“轻松”的任务。我需要创建一个字符串,并且在此字符串中,我需要用其他单词替换某些单词,而无需使用for循环:(同样我想打印它,但是我不知道将Console.WriteLine和google放在哪里小时没有工作或问大学。

/ * Excersice:与stringbuilder一起使用 *猫成为小猫 *狗变成小狗 *鼠标变成小老鼠 *必须替换为的单词 *不要使用循环* /


using System;
using System.Collections.Generic;
using System.Text;

namespace Opgavens_leerpad_3_oefening
{
    class Program
    {
        static string Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            string dogCat = new string("Can the cat find the mouse without waking the dog.");

            static string replacethisstring(string dogCat);
            {
                hondKat = dogCat.Replace("cat", "littlecat");
                hondKat = dogCat.Replace("dog", "littldog");
                hondKat = dogCat.Replace("mouse", "littlemouse");
                hondKat = dogCat.Replace("the", "a");
                return dogCat;
            }
        }
    }
}

错误CS5001:程序不包含适用于入口点的静态“ Main”方法(我不明白这不是几乎所有程序都以该静态Main args开始吗?) < / p>

错误CS8112:replacethisstring(string)'是局部函数,因此必须始终具有主体。 (我刚刚给它一个身体正确吗?我打开了{并关闭了它},然后用return替换了它。)

3 个答案:

答案 0 :(得分:3)

方法声明以;结尾,这就是 CS8112

的原因

Main方法必须返回您已将其修改为void的{​​{1}}(或'int'),这就是 CS5001

的原因

如果您希望程序在控制台上打印输出,请使用:

string

答案 1 :(得分:1)

  1. 您的main应该有一个void作为返回类型。不允许使用字符串,但可以选择使用int(see reference
  2. 在带有主体的函数声明的末尾不能包含;
  3. 必须先声明一个变量,然后才能使用它... string hondKat;
  4. 在下面的代码中查看StringBuilder的使用,而不是字符串。
namespace Opgavens_leerpad_3_oefening
{
    class Program
    {
        public static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Can the cat find the mouse without waking the dog.");
            sb = replacethisstring(sb);

            Console.WriteLine(sb.ToString());
            Console.ReadLine(); // To Stop the Console from closing.

            static StringBuilder replacethisstring(StringBuilder dogCat)
            {
                dogCat = dogCat.Replace("cat", "littlecat");
                dogCat = dogCat.Replace("dog", "littldog");
                dogCat = dogCat.Replace("mouse", "littlemouse");
                dogCat = dogCat.Replace("the", "a");
                return dogCat;
            }

        }
    }
}

您可以将功能放置在Main或外部。通常,您会在Main类之外找到函数。

    public static void Main(string[] args)
    {
    ...
    }

    public static string replacethisstring(string dogCat)
    {
    ...
    }

答案 2 :(得分:0)

有多个问题,如拼写错误,语法错误等。 此外,练习具有需要与stringbuilder一起使用的条件。

所以,尝试这个。

    static void Main(string[] args)
    {
        StringBuilder sb = new StringBuilder("Can the cat find the mouse without waking the dog?");
        sb = replacethisstring(sb);

        Console.WriteLine(sb.ToString());
        Console.ReadLine();
    }

    static StringBuilder replacethisstring(StringBuilder dogCat)
    {
        StringBuilder hondKat = dogCat.Replace("cat", "littlecat");
        hondKat = dogCat.Replace("the", "a");
        hondKat = dogCat.Replace("dog", "littledog");
        hondKat = dogCat.Replace("mouse", "littlemouse");
        return hondKat;
    }