Reputation: 399
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:
no spaces in textBox2
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
Upvotes: 1
Views: 665
Reputation: 67928
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.
Upvotes: 4
Reputation: 6963
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;
}
Upvotes: 1
Reputation: 193
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;
}
Upvotes: 1
Reputation: 122
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.
Upvotes: 0
Reputation: 8646
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;
Upvotes: 0