簡體   English   中英

字符串查找和替換

[英]String find and replace

我有一個像

string text="~aaa~bbb~ccc~bbbddd";

輸入值將是: bbb

所以在上面的字符串中,我應該刪除值“~bbb”

結果字符串應該是

text="~aaa~ccc~bbbddd";

我不確定你想做什么,但如果我有它,你可以這樣做:

private string ReplaceFirstOccurrence(string Source, string Find, string Replace)
{
 int Place = Source.IndexOf(Find);
 string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
 return result;
}

var result =ReplaceFirstOccurrence(text,"~"+input,"");

一種方法是:

string text = "~aaa~bbb~ccc~bbbddd";
string newStr = string.Join("~", text.Split('~').Where(r => r != "bbb"));

但如果性能是考慮因素,則考慮其他一些解決方案

這應該可以解決問題:

string searchValue = "bbb";
text = text.Replace(String.Format("~{0}~", searchValue), "~");

一定要搜索結尾的 ~ 字符,否則你也會替換 ~bbbddd 的一部分。

您可以在 c# 中使用正則表達式 @"\\bMYWORDTOREPLACE\\b" 這將是...

using System.Text.RegularExpressions;

myString = Regex.Replace(myString, @"\bbbb\b", "", RegexOptions.IgnoreCase);

像這樣

string str = "~rajeev~ravi";
string strRemove = "rajeev";
string strNew =str.Replace("~"+strRemove ,"");
public static string Replace(this String str, char[] chars, string replacement)
    {
        StringBuilder output = new StringBuilder(str.Length);
        bool replace = false;
        if (chars.Length - 1 < 1)
        {
            for (int i = 0; i < str.Length; i++)
            {

                char c = str[i];
                replace = false;
                //  int val = Regex.Matches(ch.ToString(), @"[a-zA-Z]").Count;


                for (int j = 0; j < chars.Length; j++)
                {
                    if (chars[j] == c)
                    {
                        replace = true;

                        break;

                    }

                }

                if (replace)
                    output.Append(replacement);
                else
                    output.Append(c);
            }
        }
        else
        {

            int j = 0;
            int truecount = 0;
                char c1 = '\0';
                for (int k = 0; k < str.Length; k++)
                {
                   c1 = str[k];

                    if (chars[j] == c1)
                    {
                        replace = true;
                        truecount ++;


                    }

                    else
                    {
                        truecount = 0;
                        replace = false;
                        j = 0;
                    }
                    if(truecount>0)
                    {
                        j++;
                    }

                    if (j > chars.Length-1)
                    {
                        j = 0;
                    }

                    if (truecount == chars.Length)
                    {
                        output.Remove(output.Length - chars.Length+1, chars.Length-1);
                       // output.Remove(4, 2);
                        if (replace)
                            output.Append(replacement);
                        else
                            output.Append(c1);
                    }
                    else
                    {
                        output.Append(c1);
                    }


                }

        }


        return output.ToString();
    }


static void Main(string[] args)
{

    Console.WriteLine("Enter a word");
    string word = (Console.ReadLine());

    Console.WriteLine("Enter a word to find");
    string find = (Console.ReadLine());

    Console.WriteLine("Enter a word to Replace");
    string Rep = (Console.ReadLine());

    Console.WriteLine(Replace(word, find.ToCharArray(), Rep));
    Console.ReadLine();

}

}

這是一個老問題,但我的解決方案是為字符串創建擴展函數。 像“.ReplaceFirst”Java 方法。

您需要創建像 Helper 這樣的靜態類,並在該類中創建靜態擴展函數:

public static class Helpers
{
    public static string ReplaceFirst(this String str, string find, string replace)
    {
        int place = str.IndexOf(find);
        if (place < 0)
        {
            return str;
        }
        //return str.Substring(0, place) + replace + str.Substring(place + find.Length);
        return str.Remove(place, find.Length).Insert(place, replace);
    }
}

用法與 .Replace 方法相同...

string text="~aaa~bbb~ccc~bbbddd";
string temp = text.ReplaceFirst("~bbb", ""); //text="~aaa~ccc~bbbddd"

或者更多

string text="~aaa~bbb~ccc~bbbddd";
string temp = text.ReplaceFirst("~bbb", "").ReplaceFirst("~bbb", ""); //text="~aaa~cccddd"

嗯,你可以做這樣的事情。
(你只需要輸入'bbb')

string text  = "~aaa~bbb~ccc~bbbddd";
string input = "bbb";                

string output = text.Replace("~" + input + "~", "~");
Console.WriteLine(output);

輸出: ~aaa~ccc~bbbddd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM