0

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.

6
  • 2
    Have you tried searching the internet for how to do this? Commented Mar 8, 2018 at 3:40
  • If you know where the space is, and you know where the slash is, the number between the two must be fixed unless the string changes. Commented Mar 8, 2018 at 3:43
  • 1
    If you want to learn a handy new skill (and some would say add another problem to the mix), have a look at regular expressions. They can do all this and a whole lot more. Otherwise you could write your own custom string parser which would be faster. Depends what you want: a multi-tool that you'll use for the rest of your life or a solution to this specific problem :) Commented Mar 8, 2018 at 3:43
  • please use string.replace(char oldchar, char newchar) Commented Mar 8, 2018 at 3:44
  • 1
    You can literally type the title of your question into Google as is and I’m sure the first 20 results would provide an answer 🙁 Commented Mar 8, 2018 at 3:47

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

You should consider using a regex

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

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

1 Comment

Anybody who knows how to get the double backslash to show up feel free to edit. Doesn't work so well trying to edit on phone.
-1

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

7 Comments

this is not an ideal solution since op said there can be many a and b
@Agent_Orange his answer is correct. He is showing "aaa" which is different from "a". More specifically he should say "aaa/" but the point is still made.
Hi, that is not what I mean. I need a universal method to replace any stuff between the slash and the space closest to it.
sorry, the characteristic for the substring I want to replace is that it is between the space and the slash. (There is only one slash. The space is the one in front of and closest to the slash).
Hi, perhaps I did not express myself correctly before. Now I have edited my question.@Sam
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.