This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

String

Help for those learning Tcl or writing their own scripts.
Post Reply
L
Lu5ck
Halfop
Posts: 43
Joined: Thu Dec 07, 2006 8:58 am

String

Post by Lu5ck »

Hi all,

How do you add text to string? Something like insert or var + "this message" in other language.

Regards,
Lu5ck
User avatar
CrazyCat
Revered One
Posts: 1253
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Using append (for strings) or concat (for lists):

Code: Select all

append var "more text"
concat var "other stuffs"
L
Lu5ck
Halfop
Posts: 43
Joined: Thu Dec 07, 2006 8:58 am

Post by Lu5ck »

Hi there,

Thanks for the answer. What if I want to add text into the center of the string or any location in the string.

Regards,
Lu5ck
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

Code: Select all

set test "This is just a test uh"
set test [concat [lrange $test 0 2] "OOPS" [lrange $test 3 end]]

Code: Select all

Tcl return: This is just OOPS a test uh
use lrange to grab what you want.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Errmmm.... NO!
First learn do tell the difference between lists and strings in tcl...
Add a { or such into the test-string and watch how your code falls apart...

Proper code would look something like this:

Code: Select all

set test "This is just a test uh"
set list1 [split $test]
set list2 [list "This" "is" "just" "a" "test" "uh"]
set result1 [join [concat [lrange $test1 0 2] "OOPS" [lrange $test1 3 end]]]
linsert list2 3 "OOPS"
Edit:
In order to accomplish the same result without resorting to lists, use something like this:

Code: Select all

set test "This is just a test uh"
set result [string replace $test 12 12 "OOPS "]
NML_375
L
Lu5ck
Halfop
Posts: 43
Joined: Thu Dec 07, 2006 8:58 am

Post by Lu5ck »

Hi there,

Thank you for the help.

Regards,
Lu5ck
L
Lu5ck
Halfop
Posts: 43
Joined: Thu Dec 07, 2006 8:58 am

Post by Lu5ck »

Hi there,

Is there a easiler way to do it if I wanted to add text infront of the string?

Regards,
Lu5ck
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

set String "the end."
set String "This is $String"
Have you ever read "The Manual"?
L
Lu5ck
Halfop
Posts: 43
Joined: Thu Dec 07, 2006 8:58 am

Post by Lu5ck »

Hi there,

Thanks.

Regards,
Lu5ck
Post Reply