Home c# How to split a line into two variables by separator?

How to split a line into two variables by separator?

Author

Date

Category

How to split the line into two variables by the separator?
There is a row Kek: Kek4chanmemamoral You need to trim everything up to : and score it in the first variable, and the rest in the second:

var 1 = kek;
var 2 = kek4chanmemamoral;

Answer 1, Authority 100%

You can using the string.split () design:

string text = "Kek2Cheburek";
String [] Words = Text.Split (new char [] {':'});
// NEW CHAR [] - an array of separators symbol. How I was corrected in
// Comments, in this case, it is enough to write to the text.split (':')
STRING FIRST = WORDS [0];
STRING SECOND = WORDS [1];
Console.WriteLine (First);
Console.Writeline (Second);

In this code, we create an array of Words strings, clog its contents of a string broken “:”. If you insert two colors – there will be three elements, insert three colors in the string – will be 4, etc.

I hope it turned out.


Answer 2

You can with regular expressions, but it is personally for me, a more complex way, unlike the cycle and substring.

string mystere = "firstpart: secondpart";
for (int i = 0; i & lt; mystring.length, i ++)
if (Compare (MyString [i], ":") == 0)
{
STRING NEWSTRING = SUBSTRING (0, I);
STRING NEWSTRING2 = SUBSTRING (I, MYSTRING.LENGTH-I);
Break;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions