a=new String("Hello");
a[0]==="H" //true
a[0]="J"
a[0]==="J" //false
a[0]==="H" //true
Does this mean I can only use Strings as arrays of char's by .split("")
and then .join("")
?
ANSWER: Yes, in Javascript strings are readonly
(aka immutable) This question is answered here at:
Use the readOnly property to check if an element is read-only, e.g. if (element. readOnly) {} . The readOnly property returns true when the element is read-only, otherwise false is returned.
The readOnly property sets or returns whether a text field is read-only, or not. A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it. Tip: To prevent the user from interacting with the field, use the disabled property instead.
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging.
Strings are immutable, so yes. a
should be reassigned if you want to change the string. You can also use slice
: a = 'j'+a.slice(1)
, or a replace
: a = a.replace(/^h/i,'j')
.
You could create a custom mutable String
object, something like this experiment (esp. see method replaceCharAt
).
Thats correct.
You can of course build a function to handle this for you.
See this SO Post for different examples of this:
How do I replace a character at a particular index in JavaScript?
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