1

I tried to do it this way in Form1:

private void BtnScrambleText_Click(object sender, EventArgs e)
{
    textBox1.Enabled = false;
    BtnScrambleText.Enabled = false;

    StringBuilder sb = new StringBuilder();
    var words = textBox1.Text.Split(new char[] { ' ' });
    foreach (var w in words)
    {
        if (w == " ")
        {
            sb.Append(w);
            continue;
        }

        ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(w);
        scrmbltb.GetText();
        sb.Append(scrmbltb.scrambledWord);
        textBox2.AppendText(sb.ToString());
    }
}

The new class i have is ScrambleTextBoxText there im just getting a word from the textBox1 scramble it randomaly and then im adding the scrambled word back to the textBox2

But in textBox2 i see all the words in one long string like:

dannyhihellobyethis

There are no spaces at all between the words. I needed it add to textBox2 with the exact spaces it was in textBox1.

If in textBox1 it was for example:

danny hello hi yes two four

moses daniel    yellow

So in textBox2 it should be the same line this:

danny hello hi yes two four

moses daniel    yellow

With same spaces with two lines down and everything.

Two problems:

  1. no spaces in textBox2

  2. its adding to textBox2 any word i typed in textBox1 but it should add only the words that return from my new class : scrmbltb.scrambledWord

For example if i entered in textBox1 : hi daniel

So in textBox2 it should be : daniel Without the word : hi

Or if in textBox1 it was : daniel hi hello So in textBox2 it will be: daniel hello

1
  • Honestly, have you tried anything yourself? Commented Jul 2, 2013 at 12:50

5 Answers 5

4

Why not split them and work with this individually? For example:

StringBuilder sb = new StringBuilder();
var words = textBox1.Text.Split(new char[] { ' ' });
foreach (var w in words)
{
    if (string.IsNullOrEmpty(w))
    {
        sb.Append(w);
        continue;
    }

    // do something with w
    sb.Append(w);
}

This algorithm would maintain all the spaces, but allow you manipulate w before appending it.

Sign up to request clarification or add additional context in comments.

3 Comments

Michael Perrenoud im getting an error on the line if (w == ' ') Operator '==' cannot be applied to operands of type 'string' and 'char'
@HaimKashi, change that to w == " ".
Edited my question since its not working good yet with the spaces and the words its adding to textBox2. please take a look at it.
1
var str = textbox1.Text.split(' ');
string[] ignoreChars = new string[] { ",", "." };

foreach(string t in str)
{
   if(!ignoreChars.Contains(t)) //by this way, we are skipping the stuff you want to do to the words
   {
     if(!int.TryParse(t)) // same here
     {
         //dosomething to t
         // t = t + "asd";
     }
   }
   textBox2.Text += " " + t;
}

Comments

1

Quick and simple:

string text = textBox1.Text;

string[] words = text.Split(new string[] { }, StringSplitOptions.RemoveEmptyEntries);

foreach (string word in words)
{
    textBox2.Text += " " + ChangeWord(word);
}

and if you don't like the leading space:

textBox2.Text = textBox2.Text.Trim();

EDIT

I just noticed that you want to change the words ad-hoc as well. In that case, see above change and add this:

private string ChangeWord(string word)
{
    // Do something to the word
    return word;
}

Comments

0

Try to do as follows:

String str=TextBox1.Text;
String[] tokens = str.split(" ");

for(int i=0;i<tokens.length();i++)
{
  String retVal = tokens[i];
}

TextBox2.Text=retVal;

Comments

0

You can use getline or readline for C# which will get the entire line in the text box and then store it in a temp variable.

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.