x


Problems initializing a built-in array of char

I am having a difficult time initializing an array of char to use in String.Trim member function in Unityscript. At a high level I am just trying to remove any trailing carriage returns from a string. This should be a no brainer but I have tried different methods to no avail -

for example this -
var charsToTrim : char[] = ['\n','\r']; chatTextAreaString.Trim(charsToTrim);

results in this error - Assets/Scripts/GameGUI.js(300,60): BCE0022: Cannot convert 'String[]' to 'char[]'.

and this - var charsToTrim : char[] = new char[1]; charsToTrim[1] = '\n'; chatTextAreaString.Trim(charsToTrim);

results in this error - Assets/Scripts/GameGUI.js(261,50): BCE0022: Cannot convert 'String' to 'char'.

The root of the issue is that I need to understand the syntax and caveats of using different types of arrays in Unityscript and haven't found a comprehensive source for this.

Any help with my immediate problem would be appreciated and if anyone has a source where I can read up on Unityscript array syntax would also be great.

more ▼

asked Apr 10 '11 at 07:06 PM

jrbailey1977 gravatar image

jrbailey1977
24 5 5 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

['\n','\r'] is an array of strings. In JS, ' and " are the same. If you want a char, you need to specify one element from a string, such as "\n"[0]. (If the string is one character long, then 0 is of course the only index that will work.)

more ▼

answered Apr 10 '11 at 07:21 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Eric's explanation is far clearer than mine :)

Apr 10 '11 at 07:29 PM Wibbs 1

Wow, thanks for the quick response Eric and Wibbs! This is what I was missing.

Apr 10 '11 at 07:57 PM jrbailey1977
(comments are locked)
10|3000 characters needed characters left

I think its got something to do with Javascript not liking character literals. Try:

var charsToTrim : char[] = ["\n"[0],"\r"[0]]; chatTextAreaString.Trim(charsToTrim);

instead.

more ▼

answered Apr 10 '11 at 07:21 PM

Wibbs 1 gravatar image

Wibbs 1
331 5 6 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x370
x28

asked: Apr 10 '11 at 07:06 PM

Seen: 1823 times

Last Updated: Apr 10 '11 at 07:06 PM