?
目录
????????使用字符串对象的ToUpper方法可以将字符串中的字母全部转换为大写。
string P_str_book ="mingribook".ToUpper();
????????使用字符串对象的ToLower方法可以将字符串中的字母全部转换为小写。
string P_str_book ="MINGRIBOOK".ToLower();
????????字符串在创建后就成为不可变的对象,当调用字符串对象的方法操作字符串时,会产生新的字符串对象,而不是更改原来的字符串对象。
????????在深入使用字符串之前,有一个概念一定要理解,字符串是不可变的对象。理解了这一概念,对后面熟练使用字符串有着很大的帮助。字符串的不可变性,意味着每当对字符串进行操作时,都将产生一个新的字符串对象,如果频繁地操作字符串对象,会在托管堆中产生大量的无用字符串,增加垃圾收集器的压力,从而造成系统资源的浪费。
// 将字母全部转换为大写或小写
namespace _035
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private TextBox? textBox2;
private RadioButton? radioButton2;
private RadioButton? radioButton1;
private Button? button1;
private TextBox? textBox1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(49, 101),
Name = "textBox2",
Size = new Size(189, 23),
TabIndex = 4
};
//
// radioButton2
//
radioButton2 = new RadioButton
{
AutoSize = true,
Location = new Point(188, 64),
Name = "radioButton2",
Size = new Size(50, 21),
TabIndex = 3,
TabStop = true,
Text = "小写",
UseVisualStyleBackColor = true
};
//
// radioButton1
//
radioButton1 = new RadioButton
{
AutoSize = true,
Location = new Point(130, 65),
Name = "radioButton1",
Size = new Size(50, 21),
TabIndex = 2,
TabStop = true,
Text = "大写",
UseVisualStyleBackColor = true
};
//
// button1
//
button1 = new Button
{
Location = new Point(49, 63),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 1,
Text = "转换",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(49, 22),
Name = "textBox1",
Size = new Size(189, 23),
TabIndex = 0,
Text = "请输入字符串",
TextAlign = HorizontalAlignment.Center
};
textBox1.MouseClick += TextBox1_MouseClick;
//
// groupBox1
//
groupBox1 = new GroupBox
{
Dock = DockStyle.Fill,
Location = new Point(0, 0),
Name = "groupBox1",
Size = new Size(289, 136),
TabIndex = 0,
TabStop = false,
Text = "大小写转换"
};
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(radioButton2);
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(textBox1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(289, 136);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "字母大小写转换";
}
private void Button1_Click(object? sender, EventArgs e)
{
if (radioButton1!.Checked)
{
textBox2!.Text = textBox1!.Text.ToUpper(); //将字符串转换为大写
}
else
{
textBox2!.Text = textBox1!.Text.ToLower(); //将字符串转换为小写
}
}
private void TextBox1_MouseClick(object? sender, MouseEventArgs e)
{
if (textBox1!.Text == "请输入字符串")
{
textBox1!.Text = string.Empty;
}
}
}
}
?
?