Replacing multiple words in a string sentence C#

Related searches

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.

/* 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*/

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? )

Error CS8112: replacethisstring(string)' is a local function and must therefore always have a body. ( I just gave it a body right? I opened the { and close it } and put a replace with return. )

The method declaration ends with ; that’s the reason for CS8112

The Main method has to return void (or ‘int’ )you’ve modified it to string, that’s the reason for CS5001

If you want the program to print the output on the console use:

using System;

....

 Console.WriteLine(output)

Replace multiple words in string, C# program that causes multiple replacements using System; class Program { static With StringBuilder, Replace works the same way as with strings, but we do� Regex.Replace. The string.Replace() method is limited in the kinds of substrings it can match. With Regex we can match met a-characters like "digit" or "non-word."Regex.Replace. Here: We replace all strings starting with the word "bird" and ending with a digit char and a non-word char (like a space).

  1. Your main should have a void as return type. string is not allowed but int is an option (see reference)
  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;
  4. See the use of StringBuilder in the below code instead of string.
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. Normally you would find functions outside of the Main class.

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

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

C# Replace String Examples, Given a Sentence having multiple strings. How to replace multiple strings in a given string in C#, Some times we need to replace more than one string or� In C#, Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it. Syntax: public string Replace(char Oldchar, char Newchar) or public string Replace(string Oldvalue, string Newvalue) Explanation:

Having several issues like typos, syntax error etc. Additionally, the excercise having a condition that needs to use with 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;
    }

Replace multiple characters in one replace call, I'm trying to replace multiple words in a string with multiple other words. multiple strings with multiple other strings Given a Sentence having multiple strings. First c# - Fastest way to replace multiple strings in a huge string - Stack Overflow. public: System::String ^ Replace(char oldChar, char newChar); public string Replace (char oldChar, char newChar); member this.Replace : char * char -> string Public Function Replace (oldChar As Char, newChar As Char) As String Parameters

Replace multiple strings with multiple other strings, The Replace method can replace either strings or single characters. In both cases, every occurrence of the sought text is replaced. Re: REGEXP_REPLACE how to replace multiple words delimited by single space? Paulzip Oct 3, 2018 9:56 PM ( in response to mathguy ) In regex, a character class defines a set of characters and / or set of combined character classes, any one of which can occur in an input string for a match to succeed.

How to modify string contents, My need is that i open a text file. search for a string, and this search i do using filestream and i search the string in each line, if the string is found in the line the, i need to replace that string with a new string. Regex give me methods to replace string but not in the file. Is there any way i can replace the string in the file.

That means if the same text string occurs many times in a Word document, we only replace a special one, such as the second one and the others remain same. By invoking FindAllString (string matchString, bool caseSensitive, bool wholeWord) method, we can find the text strings and then set the new string for the indexed one that we want to replace.

Comments
  • Main method declaration should static void Main(string[] args), note void
  • you can do the replacements in one line hondKat = dogat.Replace().Replace() etc..
  • There is one ; too much. Write static string replacethisstring(string dogCat) instead of static string replacethisstring(string dogCat);
  • Or Main can return int...but not string or anything else besides void and int.
  • I don't need the answer then why did you ask the question?
  • Thank you for the fast response ! I just noticed when i change some of these i get new errors frustrating.
  • use with stringbuilder and... :)
  • This works perfectly thank you :D Just a few questions though stop console from closing with readline? If i click debug my command screen doesnt close if i delete readline. and why did you use toString()?
  • Stop closing debug window answered here. You have to use ToString() to print a StringBuilder to the console. StringBuilder is an object and cant be printed like a string. You have to get the string representation by using ToString() method on a StringBuilder.