简体   繁体   English

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

[英]Replacing multiple words in a string sentence C#

Hello guys, I don't need the answer but I would look to know and find out what I'm doing wrong.大家好,我不需要答案,但我想知道并找出我做错了什么。 As a beginner I got a very "easy" assignment in my studies.作为初学者,我在学习中得到了一项非常“简单”的作业。 I need to create a string and inside this string I need to replace some words by other words without using the for loop as so: ( also I want to print it but I got no clue where to put Console.WriteLine and google searching for 1 hour didn't work or asking a colleage.我需要创建一个字符串,在这个字符串中,我需要用其他单词替换一些单词而不使用 for 循环:(我也想打印它,但我不知道把 Console.WriteLine 放在哪里,谷歌搜索 1小时没有工作或问大学。

/* Excersice: use with stringbuilder * cat becomes littlecat * dog becomes littledog * mouse becomes littlemouse * words the must be replace by a * do not use a loop*/ /* 练习:与 stringbuilder 一起使用 * cat 变成 littlecat * dog 变成 littledog * 鼠标变成 littlemouse * 必须用 a 替换的单词 * 不要使用循环 */


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

Error CS5001: Program does not contain a static "Main" method suitable for an entry point ( I don't get this doesn't almost any program starts with this static Main args? )错误 CS5001:程序不包含适合入口点的静态“Main”方法(我不明白几乎没有任何程序以这个静态 Main args 开始?)

Error CS8112: replacethisstring(string)' is a local function and must therefore always have a body.错误 CS8112:replacethisstring(string)' 是一个本地函数,因此必须始终有一个主体。 ( I just gave it a body right? I opened the { and close it } and put a replace with return. ) (我只是给了它一个主体,对吗?我打开 { 并关闭它 } 并用 return 替换。)

The method declaration ends with ;方法声明以;结尾; that's the reason for CS8112这就是CS8112的原因

The Main method has to return void (or 'int' )you've modified it to string , that's the reason for CS5001 Main 方法必须返回void (或 'int' )您已将其修改为string ,这就是CS5001的原因

If you want the program to print the output on the console use:如果您希望程序在控制台上打印输出,请使用:

using System;

....

 Console.WriteLine(output)
  1. Your main should have a void as return type.您的 main 应该有一个 void 作为返回类型。 string is not allowed but int is an option ( see reference )字符串是不允许的,但 int 是一个选项( 见参考
  2. You cannot have ;你不能有; at the end of a function declaration that has a body to it.在具有主体的函数声明的末尾。
  3. You have to declare a variable before you can use it ... string hondKat;您必须先声明一个变量,然后才能使用它... string hondKat;
  4. See the use of StringBuilder in the below code instead of string.请参阅以下代码中 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;
            }

        }
    }
}

You can place the function within the Main or outside.您可以将函数放置在 Main 内或外部。 Normally you would find functions outside of the Main class.通常,您会在 Main 类之外找到函数。

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

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

Having several issues like typos, syntax error etc. Additionally, the excercise having a condition that needs to use with stringbuilder.有几个问题,如拼写错误、语法错误等。此外,该练习有一个需要与 stringbuilder 一起使用的条件。

So, try this.所以,试试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM