How to separate a string in Unity c#

Asked

Viewed 187 times

2

I wanted to know how to separate a string in Unity C#, I ended up looking at some similar questions for C# punch not in Unity, I often found this example

string[] linha = reader.ReadLine().Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

and the error that the function StringSplitOptions does not exist in context, so I would like to know a method.

Here’s what I want to do, I will have in a TXT file a line of text that I want to separate by the texo for each || , Ex:

" Você acorda, o que faz?||Dormir mais||2||Levantar||1||Sentar e esperar||2||Cair da cama||3$2||1<> "

I would like to separate this text as follows:;

string lin1 = "Você acorda, o que faz?"
string lin2 = "Dormir mais"
string lin3 = "2"
string lin4 = "Levantar"
string lin5 = "1"
string lin6 = "Sentar e esperar"
string lin7 = "2"
string lin8 = "Cair da cama"
string lin9 = "3$2"
string lin10 = "1"

Note: the <> in theory is to serve as line breaking;

  • Young man, did you manage to solve your problem? The answer answered what you needed or need something improved?

1 answer

2


What happens is that Unity runs on a very old version of Mono. And in this version, some things that we are used to are not implemented.

The method Split, in this version, there is no overload that accepts as a parameter array of strings and a StringSplitOptions.

On the other hand, there is an overload that accepts a array of chars and a StringSplitOptions.

In this case, it will be impossible to break the text to each pair of Pipes (|), but how will you be using the option RemoveEmptyEntries, ends up having the same end result.

reader.ReadLine().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

See working on . NET Fiddle.

  • Thanks!! , it worked perfectly now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.