How to change Symbol Number to Upper Case... in the "richTextBox.Lines" array?
How to change to Upper Case SymbolNumber... in the "richTextBox.Lines" array? I have a problem with "ToUpper()", because it is working only with the "String" data, but I need change the case to upper by number of the symbol.
For example... I have 开发者_JAVA百科a text... "qwerty \n asdfgh \n zxcvbn" ...in the "richTextBox.Text", and I need to change case of symbol #3 in each line (e, d, c) to Upper.
I don't know what's exactly your situation, but you can expand bellow code for words , ...:
I:
var strs = richtextbox.Text.Split("\n".ToCharArray());
var items = "";
foreach(var item in strs)
{
items += new string(item.Select((x,index)=> index == 2?x.ToUpper():x)
.ToArray())
+ "\n";
}
Edit: As I understand from your comment, this would work for you:
indexUpper is input for example set it as 3:
II:
richTextBox1.Lines = richTextBox1.Lines
.Where(x => x.Length > indexUpper + 1)
.Select(s => s.Substring(0,indexUpper)
+ s[indexUpper].ToString().ToUpper()
+ s.Substring(indexUpper,s.Length - indexUpper - 1))
.ToArray();
and if you want to have all items:
III:
this.richTextBox1.Lines = richTextBox1.Lines
//.Where(x => x.Length > indexUpper + 1)
.Select(s => s.Length > indexUpper + 1? s.Substring(0,indexUpper)
+ s[indexUpper].ToString().ToUpper()
+ s.Substring(indexUpper + 1,s.Length - indexUpper - 1)
: s.Length == indexUpper + 1?
s.Substring(0,indexUpper)
+ s[indexUpper].ToString().ToUpper()
:s).ToArray();
Try this code:
public Form1()
{
InitializeComponent();
string text = "qwerty\nasdfgh\nzxcvbn";
richTextBox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
//change ever 3rd charater in each line:
string[] words = richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
richTextBox1.Text = "";
for (int i = 0; i < words.Length; i++)
{
char[] chars = words[i].ToCharArray();
chars[2] = Convert.ToChar(chars[2].ToString().ToUpper());
string newWord = new string(chars);
richTextBox1.AppendText(newWord + "\n");
}
}
精彩评论