I have an array of type object
which are strings. I would like to convert them to strings. What would be the quickest way of doing so?
Eg.: I have this object[]
and want to convert it so it is this string[]
.
UPDATE: I think the problem is that some of the objects on the object[]
are actually other objects like integers. I would need to convert them to strings first. Please include that into your solution. Thanks.
To convert a JavaScript array into a string, you can use the built-in Array method called toString . Keep in mind that the toString method can't be used on an array of objects because it will return [object Object] instead of the actual values.
Java For Testers toArray() returns an Object[], it can be converted to String array by passing the String[] as parameter.
object[] data = new object[] { "hello", "world", "!" };
string[] stringData = data.Cast<string>().ToArray();
If your object array contains mixed elements you can use the ConvertAll
method of Array
:
object[] data = new object[] { "hello", 1, 2, "world", "!" };
string[] stringData = Array.ConvertAll<object, string>(data, o => o.ToString());
string[] output = Array.ConvertAll(objects, item => item.ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With