replace part of the string in C#

早过忘川 提交于 2020-06-13 12:16:23

问题


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.
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.

How do I locate this substring so that I can replace it with the new substring I want? Thanks.


回答1:


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




回答2:


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



回答3:


Use 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");



回答4:


You should consider using a regex

Expression could be something like this "([^/]+)\\s(\\w+)/(.+)"

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



来源:https://stackoverflow.com/questions/49164968/replace-part-of-the-string-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!