[ C# Scripting ] - Split a String

Hi

I’m trying split a string, but I have a error message. What is it wrong?

//Split
string n_s = "John_Smith";
string name = "";

void Start () {

  //Split
  string[] splitArray =  n_s.Split(n_s,char.Parse("_")); //Here we assing the splitted string to array by that char
  name = splitArray[0]; //Here we assign the first part to the name
}

Thank You

1 Like

What is the error message???

If you are just splitting by the underscore, n_s.Split(‘_’); will do that. That creates two strings. Just note the single quotes.

lol, @Kiwasi

string[] splitArray =  n_s.Split(char.Parse("_"));
name = splitArray[0];

Edit: The error was “blah, blah closest overload match has invalid arguments” :wink:

3 Likes

It’s totally obvious once I see the error message. Error messages are our friends. And we all need more friends.

:wink:

2 Likes

Indeed. Just cracked me up because of how people say they have an error but don’t write what it is :slight_smile:

4 Likes

Thank You

Can I split a XML by name ?

Example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<enemys>
  <node>
    <name>Test 1</name>
  </node>
  <node>
    <name>Teste 2</name>
  </node>
</enemys>

Thank You

It’d be better to use an xml parser. (assuming you have to use xml)

2 Likes

ok