Message Boards Message Boards

0
|
4437 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Increment number in string using StringReplace?

Posted 8 years ago

(* how to transform "abc2" into "abc3" ? *)

string = "abc2";

regex = ".$";

newString = 
 StringReplace[string, 
  RegularExpression[regex] -> IntegerString[1+$0 ]]

(* output expected: "abc3" - but output is:

"abc"~~IntegerString[1+$0]

*)

POSTED BY: Elliot Asturian
2 Replies

(* solved:

:> not -> ,the latter,Rule,is evaluated immediately *)

string = "abc2";

regex = ".$";

newString = StringReplace[string, RegularExpression[regex] :> ToString[1 + FromDigits["$0"]]]

(* output is: "abc3" *)

POSTED BY: Elliot Asturian
In[1]:= string = "abc2";

In[2]:= StringJoin[StringDrop[string, {4}], 
 ToString[(ToExpression@StringPart[string, 4] + 1)]]

Out[2]= "abc3"
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract