简体   繁体   English

替换C#中的部分字符串

[英]replace part of the string in C#

I have many strings which have multiple spaces and one forward slash like this: 我有很多具有多个空格和一个正斜杠的字符串,如下所示:

string a="xxxxxx xxx xxxxxxxx xxxxxx aaa/xxxxxxxxxxxxxxx";

or 要么

string b="xxxx xxx xxxxxx bbbbb/xxxxxxx xxx";

or 要么

string c="xx xx 12345/x xx"

What I need to do is to replace the substring "aaa" or "bbbbb" or "12345" ( Please note that the substrings are just examples, they can be anything ) with the new string I want. 我需要做的是用我想要的新字符串替换子字符串“ aaa”或“ bbbbb”或“ 12345”( 请注意,子字符串只是示例,它们可以是任何东西 )。
The feature for the substring "aaa" or "bbbbb" or "12345" or anything is that the substring is right before the only one forward slash and right after the space in front of and closest to this slash. 子字符串“ aaa”或“ bbbbb”或“ 12345”或任何其他字符的功能是,该子字符串恰好在唯一的一个正斜杠之前,紧接在此斜杠前面和最接近的空格之后。

How do I locate this substring so that I can replace it with the new substring I want? 如何找到该子字符串,以便可以将其替换为所需的新子字符串? Thanks. 谢谢。

although a proposed answer is selected. 尽管选择了建议的答案。 I still decide to answer my own question. 我仍然决定回答我自己的问题。

string substr=""//any string I want
string a=OldString.split('/');//split by '/'
string b=a[0].Substring(0,a[0].LastIndexOf(' '));//get the substring before the last space
string NewString= b+ substr + a[1];//the substring between the space and the '/' is now replaced by substr

Well, well well 好吧好吧

Take your universal method: 采用通用方法:

        public string Replacement(string input, string replacement)
        {
            string[] words = input.Split(' ');
            string result = string.Empty;

            for (int i = 0; i < words.Length; i++)
            {
                if(words[i].Contains('/'))
                {
                    int barIndex = Reverse(words[i]).LastIndexOf('/') + 1;
                    int removeLenght = words[i].Substring(barIndex).Length;
                    words[i] = Reverse(words[i]);
                    words[i] = words[i].Remove(barIndex, removeLenght);
                    words[i] = words[i].Insert(barIndex, Reverse(replacement));
                    string ToReverse = words[i];
                    words[i] = Reverse(ToReverse);

                    break;
                }
            }

            for(int i = 0; i < words.Length; i++)
            {
                result += words[i] + " ";
            }

            result = result.Remove(result.Length - 1);

            return result;
        }

        public string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

In reponse to >> I need a universal method to replace any stuff between the slash and the space closest to it 响应>>我需要一种通用方法来替换斜杠和最接近它的空间之间的所有内容

Use string.Replace("", ""); 使用string.Replace(“”,“”);

string a="xxxxxx xxx xxxxxxxx xxxxxx aaa/xxxxxxxxxxxxxxx";
string b="xxxx xxx xxxxxx bbbbb/xxxxxxx xxx";

string resultA = a.Replace("aaa", "Your replacement");
string resultB = b.Replace("bbbbb", "Your replacement");

You should consider using a regex 您应该考虑使用正则表达式

Expression could be something like this "([^/]+)\\\\s(\\\\w+)/(.+)" 表达式可能类似于"([^/]+)\\\\s(\\\\w+)/(.+)"

Regex.Replace (yourString, "([^/]+)\\\\s(\\\\w+)/(.+)", "$1 replacement/$3") Regex.Replace Regex.Replace (yourString, "([^/]+)\\\\s(\\\\w+)/(.+)", "$1 replacement/$3") Regex.Replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM