Rothmanberger
Rothmanberger

Reputation: 175

Replacing string characters in c#

I have a string which is a sentence or two long (more than one word). In that sentence there will be a hash-tagged word e.g. #word. This needs to be replaced with *word*.

If the sentence is:

Today the weather is very nice #sun

It should become:

Today the weather is very nice *sun*

How would I go about doing this?

Upvotes: 2

Views: 962

Answers (4)

CodeCamper
CodeCamper

Reputation: 6984

No fancy functions or libraries just common sense and solves your problem. It supports as many hashed words as you would like.

To demonstrate how it works I have created 1 TextBox and 1 Button in a c# form but you could use this code in console or pretty much anything.

      string output = ""; bool hash = false;
        foreach (char y in textBox1.Text)
        {
            if(!hash) //checks if 'hash' mode activated
            if (y != '#') output += y; //if not # proceed as normal
            else { output += '*'; hash = true; } //replaces # with *
            else if (y != ' ') output += y; // when hash mode activated check for space
            else { output += "* "; hash = false; } // add a * before the space
        } if (hash) output += '*'; // this is needed in case the hashed word ends the sentence
        MessageBox.Show(output);

and behold

Today the weather is very nice #sun 

becomes

Today the weather is very nice *sun*

here is the same code but in method form for you to pop right in your code

   public string HashToAst(string sentence)
    {
        string output = ""; bool hash = false;
        foreach (char y in sentence)
        {
            if (!hash)
                if (y != '#') output += y;
                else { output += '*'; hash = true; } // you can change the # to anything you like here
            else if (y != ' ') output += y;
            else { output += "* "; hash = false; } // you can change the * to something else if you want
        } if (hash) output += '*';                 // and here also
        return output;
    }

to demonstrate how you could modify this below is a customizable version

    public string BlankToBlank(string sentence,char search,char highlight)
    {
        string output = ""; bool hash = false;
        foreach (char y in sentence)
        {
            if (!hash)
                if (y != search) output += y;
                else { output += highlight; hash = true; }
            else if (y != ' ') output += y;
            else { output += highlight+" "; hash = false; }
        } if (hash) output += highlight;
        return output;
    }

So the search would search for the character before the word and the highlight char will surround the word. Word being defined as characters until it reaches a space or the end of the string.

Upvotes: 0

mck
mck

Reputation: 988

You can try this

 string text = "Today the weather is very nice #sun";

 int startindex = text.Indexof('#'); 

 int endindex = text.IndexOf(" ", startIndex);

 text = text.Replace(text.substring(startIndex, 1), "*")
 text = text.Replace(text.substring(endindex, 1), "*")

Upvotes: 1

rhughes
rhughes

Reputation: 9583

Try this:

string theTag = "sun";
string theWord = "sun";

string tag = String.Format("#{0}", theTag);
string word = String.Format("*{0}*", theWord);

string myString = "Today the weather is very nice #sun";
myString = myString.Replace(tag, word);

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149058

You could do a regular expression, like this:

var output = Regex.Replace(input, @"#(\w+)", "*$1*");

Upvotes: 8

Related Questions