0

I'm programming in JScript.NET which is similar to C# . I want to split a string on multiple characters, in this case " - ".

The problem is when I do that like this (which should be the way to do it according to this thread):

var text = "test - test2";

var array = [" - "];

var val = text.Split(array, StringSplitOptions.None);

I get "Ambiguous match found". This is because the String class has both a Split(Char[], StringSplitOptions) and a Split(String[], StringSplitOptions) function, and the compiler doesn't know which one to use.

So my question is then. How do I tell the compiler that I'm using a string array when the arrays in JScript.NET are dynamically typed?

Edit: As far as I know, JScript.NET use the same APIs as C#. So this is the String class I'm using. However, I think the syntax is the same as JavaScript. Maybe someone could confirm this?

Edit2: So if there is a way to enforce a type in JScript.NET so the compiler knows which type is used, I guess that would be the answer for my case as well? JScript.NET does not have the same syntax as C#.

Cheloide
  • 793
  • 6
  • 21
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48
  • 2
    The code you posted is for C#. – Cheloide Dec 17 '18 at 18:26
  • With plain Javascript, you should be able to split your string calling `text.split(array);` – Cheloide Dec 17 '18 at 18:30
  • @Cheloide Well this is the code I use in JScript, which I thought had the same syntax as JavaScript (according to Wikipedia, I don't really have any experience with JavaScript myself). The String class that is used is this: https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2, which I guess, as you point out, is the same as in C#. – Jonatan Stenbacka Dec 18 '18 at 08:45
  • There are no types in JavaScript, so JScript is probably less similar to JavaScript that you might think. – dquijada Dec 18 '18 at 09:02
  • Jscript is a dynamically typed language as far as I know. That's why I'm really confused about the function saying that it's input is ambiguous. – Jonatan Stenbacka Dec 18 '18 at 09:05
  • @JonatanStenbacka [JScript](https://en.wikipedia.org/wiki/JScript) is not [JScript.net](https://en.wikipedia.org/wiki/JScript_.NET) – Cheloide Dec 18 '18 at 12:49
  • @Cheloide Aha... Thanks for clarifying that! I'll edit the question. – Jonatan Stenbacka Dec 18 '18 at 14:53

1 Answers1

2

I figured it out once I realized I was coding in JScript.NET and not JScript, which led me to a bunch of useful guides. One of them specifically mentioned how to create typed arrays.

It turns out it was a easy as this:

var array : String[] = [" -"];
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48