-1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;

            if (textBox1.Text.Contains("l"))
            {
                textBox1.Text.Replace("l", "s");
            }
            string nameA = textBox1.Text;
            MessageBox.Show(nameA);
        }
    }
}

基本上,我想要做的是,让用户输入一个名称,将名称中的“l”字符更改为“s”。并在按下按钮时将结果显示在消息框中。但是,无论我尝试了什么,“l”都不会改变。

编辑:谢谢大家,我不敢相信这是愚蠢的事情。哇V_V

4

3 回答 3

4
textBox1.Text = textBox1.Text.Replace("l", "s");
于 2012-06-29T04:21:34.220 回答
2

Since strings are immutable you have assign back the text after doing the replace.

Also you can just use textBox1.Text = textBox1.Text.Replace("l", "s"); and avoid the Contains check, since if the replacement is not found in the string Replace will return the original text.

于 2012-06-29T04:31:44.063 回答
0
if (name.Contains("l"))
{
    name = name.Replace("l", "s");
    textBox1.Text = name;
}

MessageBox.Show(name);
于 2012-06-29T04:23:22.193 回答