Since strings are immutable in Julia, and immutable means you can't change
them, you can't change the characters in a string whilst keeping them of
type string.
If you want to be able to change the characters directly, you need to use
something other than a string, e.g. an array of characters as explained
above.
Here is an example using byte arrays:
a = b"abcd"
for i in 1:length(a) # <--- note I corrected an error in your original code
a[i] += 1
end
println(String(a))
Technically, the right way to deal with immutable objects is to just create
a new one every time you want to change something. The compiler can then
recognise that all you are doing is changing a string and it can decide to
actually do the mutation behind the scenes automatically. On the other
hand, I wouldn't necessarily expect this to be the case with Julia
currently.
As an example:
a = "abcd"
for i in 1:length(a)
a ="$(a[1:i-1])$(a[i] + 1)$(a[i + 1:end])"
end
println(a)
Here I've used "string interpolation" and "ranges" to generate a new string
from parts of the existing one.
I'm not suggesting this is a good way of doing it. I'm just demonstrating
that this is a "functional" way of doing things, using immutables instead
of the mutable strings we are used to in C.
Bill.
On Wednesday, 17 August 2016 19:52:47 UTC+2, Rishabh Raghunath wrote:
>
> Thank you for the immense support from this amazing group !!.. I am a
> beginner in Julia and I am more familiar with the C language.. Now.. I
> understand strings are immutable..
> I am actually learning Julia for use in a programming contest and love the
> experience so far..
> There are often questions that require you to manipulate the characters
> comprising the string. Its easy to do in C as strings are nothing but an
> array of Characters and you can play around with it.. C is the online
> language I know well.. So I do not know about other languages..
> But How do I such manipulation with the individual characters of a string
> while still maintaining the type as a string.. You said its considered a
> bad Idea to take this route...
> Please tell me how I should go about manipulating characters in Julia the
> right way while the type is string..?
> For Example: Writing a Encrypting program requires me to modify the
> characters in the string..
> I tried using Character Array but .. It prints out as separate characters
> instead of a string
>
> On Wed, Aug 17, 2016 at 10:48 PM, ggggg <[email protected] <javascript:>>
> wrote:
>
>> Isn't it a red herring to say that strings lack setindex because they are
>> immutable? I think strings don't have setindex! because it is considered to
>> be a bad API choice, at least partially because you can't do O(1) indexing
>> for many string encodings.
>>
>> I can easily define a setindex! method* that does what the OP is
>> expecting, so how can this be related to strings being immutable?
>>
>> *julia> **Base.setindex!(s::ASCIIString, v,i) = s.data[i]=v*
>>
>> *setindex! (generic function with 58 methods)*
>>
>> *julia> **a="abc"*
>>
>> *"abc"*
>>
>> *julia> **a[1]='7';a*
>>
>> *"7bc"*
>>
>> *julia> **a[1]+=1;a*
>>
>> *"8bc"*
>>
>> *This works in 0.4, and I'm lead to believe this is considered a bad idea.
>>
>> On Wednesday, August 17, 2016 at 12:40:36 AM UTC-6, Jacob Quinn wrote:
>>>
>>> Strings are immutable (similar to other languages). There are several
>>> different ways to get what you want, but I tend to utilize IOBuffer a lot:
>>>
>>> a = "abcd"
>>> io = IOBuffer()
>>>
>>> for char in a
>>> write(io, a + 1)
>>> end
>>>
>>> println(takebuf_string(io))
>>>
>>> -Jacob
>>>
>>> On Wed, Aug 17, 2016 at 12:30 AM, Rishabh Raghunath <[email protected]
>>> > wrote:
>>>
>>>>
>>>> Hello fellow Julia Users!!
>>>>
>>>> How do you manipulate the individual characters comprising a string in
>>>> Julia using a for loop ?
>>>> For example:
>>>> ###################
>>>>
>>>> a = "abcd"
>>>>
>>>> for i in length(a)
>>>> a[i]+=1
>>>> end
>>>>
>>>> print(a)
>>>>
>>>> ####################
>>>> I am expecting to get my EXPECTED OUTPUT as " bcde "
>>>>
>>>> BUT I get the following error:
>>>> ##########################################
>>>>
>>>> ERROR: MethodError: `setindex!` has no method matching
>>>> setindex!(::ASCIIString, ::Char, ::Int64)
>>>> [inlined code] from ./none:2
>>>> in anonymous at ./no file:4294967295
>>>>
>>>> ##########################################
>>>> I also tried using:
>>>>
>>>> for i in eachindex(a) instead of the for loop in the above program ..
>>>> And I get the same error..
>>>>
>>>> Please tell me what i should do to get my desired output ..
>>>> Please respond ASAP..
>>>> Thanks..
>>>>
>>>
>>>
>