9 Replies - 634 Views - Last Post: 27 April 2012 - 02:54 PM Rate Topic: -----

#1 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

String Manipulation Issue

Posted 26 April 2012 - 10:59 AM

Basically I'm trying to take one string provided by the user and place pre-defined strings on each side of each character in the provided string.
I've been trying different things for a couple hours now but I just can't seem to figure it out.

For example if a user provides the following string:
"Hello"

I want to manipulate it so that the output is something like this:
"$H#$e#$l#$l#$o#"

Any ideas as to how I can accomplish something like this?
Any and all help is greatly appreciated. :)

Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation Issue

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: String Manipulation Issue

Posted 26 April 2012 - 11:02 AM

First thing that comes to mind would be to make use of the StringBuilder class.

You could split the string into an array of chars, loop over each element in the array, for each iteration adding the delimiter and the corresponding char to a StringBuilder.

Then finally just convert the StringBuilder back into a string.
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4479
  • View blog
  • Posts: 7,801
  • Joined: 08-June 10

Re: String Manipulation Issue

Posted 26 April 2012 - 11:10 AM

You don't even have to split the string, you can treat the string as an IEnumerable<char>.

foreach(char c in myString){ //...

Was This Post Helpful? 4
  • +
  • -

#4 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: String Manipulation Issue

Posted 26 April 2012 - 07:54 PM

Thanks you too, I sort of figured it out now.

Only problem now is that if the input string is somewhat long the loop I have set up seems to finish before it should... Here's part of what I have now I'm on my iPhone so I don't really feel like typing out everything lol. I'm also not sure if this forum supports code tags but just in case it does I'm using them lol.


private void multiColor()
{
outputBox.Text = "";
char[] tmpArray = inputBox.Text.ToCharArray();
for (int i = 0; i < tmpArray.Length; i++)
 {
 if (tmpArray[counter] != ' '
  {
  if (colourCounter == 1)
   {
   outputBox.Text += "[color=#"+colour1+"]" + tmpArray[counter].ToString() + "[/color]
   }
  }
 }
}



Alright the only thing missing from that are the other colourCounter checks, the only difference is that is uses a different color and on the fifth colourCounter check, which is the last one, it resets the colorCounter to 0... Wait... I think that's my problem lol I feel stupid now. I need to reset it to 1, not 0.

Nevermind, that didn't solve the problem. For example I entered "hello there everyone how's it going?" into the inputBox and the output was only "hello there everyone"... :(
Was This Post Helpful? 0
  • +
  • -

#5 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: String Manipulation Issue

Posted 26 April 2012 - 10:40 PM

Sorry for the double post but I don't see an edit post button, I guess it's because I'm in the Lo-Fi version of the site but I can't figure out how to get out of that version so yeah. x)

Here's the whole bit of code I currently have and, as I said in the post above, the loop seems to end before it should. :\
private void multiColor()
        {
        outputBox.Text = "";
        char[] tmpArray = inputBox.Text.ToCharArray();
        MessageBox.Show(tmpArray.Length.ToString());
        for (int i = 0; i < tmpArray.Length; i++)
         {
         if (tmpArray[counter] != ' ')
          {
          if (colourCounter == 1)
           {
           outputBox.Text += "[color=#" + colour1 + "]" + tmpArray[counter].ToString() + "[/color]";
           }
          if (colourCounter == 2)
           {
           outputBox.Text += "[color=#" + colour2 + "]" + tmpArray[counter].ToString()  + "[/color]";
           }
          if (colourCounter == 3)
           {
           outputBox.Text += "[color=#" + colour3 + "]" + tmpArray[counter].ToString()  + "[/color]";
           }
          if (colourCounter == 4)
           {
           outputBox.Text += "[color=#" + colour4 +"]" + tmpArray[counter].ToString() + "[/color]";
           }
          if (colourCounter == 5)
           {
           outputBox.Text += "[color=#" + colour5 + "]" + tmpArray[counter].ToString()  + "[/color]";
           colourCounter = 1;
           }
          }
         else
          {
          outputBox.Text += " ";
          }
         colourCounter += 1;
         counter += 1;
         }
        counter = 0;
        colourCounter = 1;
        }


Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4479
  • View blog
  • Posts: 7,801
  • Joined: 08-June 10

Re: String Manipulation Issue

Posted 27 April 2012 - 05:49 AM

Put in a breakpoint and step through your loop. Inspect the variables. You'll see what's going on much easier that way.
Was This Post Helpful? 0
  • +
  • -

#7 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: String Manipulation Issue

Posted 27 April 2012 - 10:25 AM

Thank you Curtis, that helped a lot. I was able to see that the colourCounter variable was going past 5 after a certain point and therefore nothing was being done inside the loop.

I fixed it by adding an extra check to see if colorCounter is equal to 5 and if it is the colorCounter is reset to 1.

I was making this for a friend who likes to make his text really colorful on forums with BB code but it gets tiring having to do it manually, this loops through 5 different colors, that you can choose, and adds the current one, based on the colorCounter, to each character along the way. :P
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4479
  • View blog
  • Posts: 7,801
  • Joined: 08-June 10

Re: String Manipulation Issue

Posted 27 April 2012 - 11:32 AM

Debugging skills are the most important thing you can learn about programming. It helps you solve all kinds of problems, and makes you feel like a genius.

If I may offer a bit more constructive criticism:

Next, try to condense your logic so it's not dependent on dozens of if-checks.

Stacking a bunch of if statements like that is considered kinda sloppy. One way to clean it up is to use a switch statement.

Another way would be to put your color variables in an array, and use another loop variable to walk through the colors.
Was This Post Helpful? 1
  • +
  • -

#9 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: String Manipulation Issue

Posted 27 April 2012 - 12:27 PM

Hahaha yes thank you for the suggestions, I actually did replace the if statements with a switch statement after getting everything to work properly. :P

I've only been using C# for about 3 and a half months so any constructive criticism is appreciated as well, thank you. :)

I hate when my code is sloppy, I've gotten lost in some of my larger projects multiple times lol. xD
Was This Post Helpful? 0
  • +
  • -

#10 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: String Manipulation Issue

Posted 27 April 2012 - 02:54 PM

http://localhostr.co..._CS_Opacity.rar

There's my source code if anyone wants to look at it or whatever. :P

Here's an example of what this does. n_n
Much better than having to do it manually that's for sure lol. xD
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1