MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Use strings in Mathematica, like in C?


Hi,

> int main() {
> 
>  char message[16] = "Hello!";
>  message[n]='a';
> 
> /* ... other code ... */
>  return(0);
> 
> }
> 
> This mean: Change the (n+1)th character (in C, array starts at 0) to the
> specified character.
> 
> Q: How can I obtain similar effect in Mathematica?

This would be one possibility of many:

StringReplacePart["hallo","e",{2,2}]

anyway, this is not replacing the string "in place" so is not an exact
equivalent of your C-Code. If you do complicated operatons on strings you
might want to either check all the String*-Functions that you can find by
evaluating:

?String*

and also look at the RegularExpressions that are new in version 5.1. If your
strings are large and/or in place operations are important, you might need
to convert your strings to a list of characters. This is not the same in
Mathematica (unlike in C) and it is not perfectly efficient as such. But to
my knowledge it is the only possibility to do in place replacements. Check
the following:

In[14]:= a=Characters["hello"]

Out[14]= {h, e, l, l, o}

In[15]:= a[[2]]="e"

Out[15]= e

In[16]:= a

Out[16]= {h, e, l, l, o}


I hope that's correct and helps..

albert


  • Prev by Date: Re: Displaying Solution in Matrix Form
  • Next by Date: Re: Displaying Solution in Matrix Form
  • Previous by thread: Re: Use strings in Mathematica, like in C?
  • Next by thread: Re: Use strings in Mathematica, like in C?