1

I have 3D model, that is represented as an HTML element with COORDS attribute having "x y z ... x y z" coordinates. If model is small (COORDS value is relatively short) then its no problem to split the string (put values into array), change specific values (i know position/index), and join array (get string back together).

So, is there a way to get/update parts of COORDS value, while only knowing position/index in the string?

1 Answer 1

1

You could use the substr function to split the string at the position where you want to change it. This way you would only end up with two part string.

Basically something like this:

var str = 'foo bar baz';
var start = str.substr(0, 4); //start is now 'foo '
var end = str.substr(7); //end is now ' baz'

var newStr = start + 'hello' + end; //foo hello baz

For this to work you would need to know the exact index in the string plus the length of the data you wish to replace. It may be easier to just split it as long as there are no performance problems with it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Jani, bus this won't work because values could be very different: "1.234 1.0 -1111.00888 ... 0 0 0". I thought about something like getAttribute("COORDS")[0], but instead of one character it would refer to whole number. I tried regular expressions, but it does the same as split function.
If you know the index it would have in the array when split, you could use indexOf (str.indexOf) in a loop to find the Nth space, and after that, the next space. These points could then be used with the substr function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.