N
N
noytmant2020-03-01 15:19:49
C++ / C#
noytmant, 2020-03-01 15:19:49

How to get multiple parts of text from string?

Let's say I have the following:

string long = "kfkfkkfppepd0d03k31word=1<3!oleg!3>1flakoekfml2laz[-1dom3-]"

And I create one more variable:
string long_part1 = "";
string long_part2 = "";

How to make long_part1 fit word oleg from long and long_part2 fit word dom?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-03-01
@noytmant

class Program {

        static void Main(string[] args) {

            string longer = "kokokoko<!3oleg!3>lololo[2!dom2!]";

            string name = SearchWordInMassive(longer, "!3");  //longer1
            string adress = SearchWordInMassive(longer, "2!"); //longer2 
        }
        
        private static string SearchWordInMassive(string massiveWords,string restrictionCharacters) {
            string word = "";
            int startIndex = massiveWords.IndexOf(restrictionCharacters) + restrictionCharacters.Length;
            int lastIndex = massiveWords.LastIndexOf(restrictionCharacters);
            int lengthWord = lastIndex - startIndex;
            word = massiveWords.Substring(startIndex, lengthWord);
            return word;
        }
    }

Maybe it could be smaller, so I'm not sure.
An array of words is passed to the method, letters - 1 parameter, 2 is what your words are limited to. It is necessary that the limiters on the sides are the same, that is, !3 oleg !3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question