U
U
Uncle Zhenya2021-10-20 08:36:47
C++ / C#
Uncle Zhenya, 2021-10-20 08:36:47

How to cut text before and after a certain word?

There is text:
"Class: Warrior
Weapon: Ax
Strength: 55"
How can I cut the text so that only "Weapon: Axe" remains?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Yurchenkov, 2021-10-20
@EPIDEMIASH

If you want to trim certain characters, you can use Trim (), the beginning of the text through Contains, the end and the beginning can still be through text.Substring. Again, this is if you don't need to search for a specific character or word to trim against.
In your case, does the text before and after have the same number of characters?

B
BasiC2k, 2021-10-20
@BasiC2k

regular expression.
Three lines of code with great potential.

K
Konstantin Tsvetkov, 2021-10-20
@tsklab

How to cut text before and after a certain word?
You understand that only this word will remain. Weapon: Axein your example. This is the result.

I
Ipatiev, 2021-10-20
@Fockker

string phrase = @"Class: Warrior
Weapon: Axe
Strength: 55";
string[] words = phrase.Split(Environment.NewLine.ToCharArray());
Console.WriteLine(words[1]);

It's amazing that the only answer was written in the comments to the question, and as many as three answers were written by some freaks who either did not understand the question, or do not understand what they themselves wrote, or both.
Despite the fact that without knowing a word in seasharp, I stupidly got the required code with the help of Google in 3 minutes.
Hence the question: are you all like that here? "Large software specialists" who, not only cannot write a simple three-line code, are not even able to distinguish between an answer to a question and a comment?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question