2

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?

1
  • This is an overly duplicated question Commented Jun 4, 2013 at 1:10

4 Answers 4

8

You could do a regular expression, like this:

var output = Regex.Replace(input, @"#(\w+)", "*$1*");
Sign up to request clarification or add additional context in comments.

6 Comments

How does that get an * at the end of the word though?
@user2256772 I didn't see your edit about SO stripping out your asterisks. See my updated answer.
How about adding a \b at the end of the pattern?
What is the \b for? p.s.w.g your answer works fine on the text so far. Thanks !
@user2256772 \b ensures the pattern ends in a word boundary.
|
1

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), "*")

Comments

0

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

2 Comments

Problem is the word could be anything, not just sun
@user2256772 If you use the String.Format to create the tag to replace, like in the above code, then it will work fine
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.