C#中怎样从指定字符串中查找并替换字符串?

2024-10-28 12:35:59
推荐回答(3个)
回答1:

stringtxt="asdjsfk/aaaaaaaaa";

判断这个字符串中包含替换/,

就在斜杠前面加上字符‘x’

1、使用if语句

if(txt.Contains("/")){txt=txt.Replace("/","x/");}

2、使用条件运算符(?:)

txt=txt.Contains("/")?txt.Replace("/","x/"):txt;

扩展资料

stringReplace(替换的目标,替换的字符串),返回值是替换后的字符串

stringstr="11xx1111x";

Console.WriteLine("原字符串是:{0}",str);

Console.Write("请输入需要替换的字符串:");

stringoldStr=Console.ReadLine();

Console.Write("请输入替换的字符串:");

stringnewStr=Console.ReadLine();

str=str.Replace(oldStr,newStr);

Console.WriteLine("替换后的字符串:{0}",str);

回答2:

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;
#region             
#endregion
namespace Find
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string str = "";
            richTextBox1.Text = str;
        }
        int start = 0;
        int count = 0;
        /// 
        /// 查找字符串
        /// 

        /// 
        /// 
        private void find_Click(object sender, EventArgs e)
        {
            string str1;
            str1 = txt_find.Text;
            if (start >= richTextBox1.Text.Length)
            {
                MessageBox.Show("以查找到尾部");
                start = 0;
            }
            else
            {
                start = richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase);
                if (start == -1)
                {
                    if (count == 0)
                    {
                        MessageBox.Show("没有该字符!");
                    }
                    else
                    {
                        MessageBox.Show("以查找到尾部!");
                        start = 0;
                    }
                }
                else
                {
                    start = start + str1.Length;
                    richTextBox1.Focus();
                }
            }
        }
        /// 
        /// 替换字符串
        /// 

        /// 
        /// 
        private void replace_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = richTextBox1.Text.Replace(txt_find.Text, txt_replace.Text);
        }
        /// 
        /// 输入查找的字符串
        /// 

        /// 
        /// 
        private void txt_find_TextChanged(object sender, EventArgs e)
        {
            string str1;
            str1 = txt_find.Text;
            start = 0;
            count = 0;
        }
    }
}
实验结果:

回答3:

/// 查找字符串 /// /// /// private void find_Click(object sender, EventArgs e){string str1; str1 = txt_find.Text; if (start >= richTextBox1.Text.Length){MessageBox.Show("以查找到尾部");start = 0;}else{start = richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase); if (start == -1){if (count == 0){MessageBox.Show("没有该字符!");}else{MessageBox.Show("以查找到尾部!");start = 0;}}else{start = start + str1.Length; richTextBox1.Focus();}}}///

/// 替换字符串 /// /// /// private void replace_Click(object sender, EventArgs e){richTextBox1.Text = richTextBox1.Text.Replace(txt_find.Text, txt_replace.Text);}/// /// 输入查找的字符串 /// /// ///