I would like to delete out the last character (not necessarily new
line) off of a string. Is there an easy way to do this "chop" in
tcl?
Thanks
% string replace foo end end
fo
--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|
> I would like to delete out the last character (not necessarily new
> line) off of a string. Is there an easy way to do this "chop" in
> tcl?
set v [string range $v 0 end-1]
> Thanks
At your pleasure ;-).
Eckhard
string trim string ?chars?
Returns a value equal to string except that any leading or
trailing characters from the set given by chars are removed. If chars
is not specified then white space is removed (spaces, tabs, newlines,
and carriage returns).
string trimleft string ?chars?
Returns a value equal to string except that any leading characters
from the set given by chars are removed. If chars is not specified
then white space is removed (spaces, tabs, newlines, and carriage
returns).
string trimright string ?chars?
Returns a value equal to string except that any trailing
characters from the set given by chars are removed. If chars is not
specified then white space is removed (spaces, tabs, newlines, and
carriage returns).
I notice that you say "Not necessarily a newline" does this mean you
wish to
keep a newline and then delete the next non-newline character? Perls
chop does
not do this as it just remove the last character regardless of what it
is. I have
coded up this chop procedure to take a list of ignorable characters.
The ignored
characters are added back to the return string by default. By default
it will not
ignore characters(as the ignorechar list is empty) and it will not
remove any that
it has skipped over. Also this because I used the "in" operator this
is Tcl8.5; but
can be easily converted to 8.4 using lsearch.
proc chop { s { ignorechars {}} { truncate 0 } } {
set idx 0
set postbuff ""
while { [string index $s end-$idx ] in $ignorechars } {
set postbuff "[string index $s end-$idx ]$postbuff"
incr idx;
}
incr idx
if { $truncate } {
return [string range $s 0 end-$idx ]
}
return "[string range $s 0 end-$idx ]$postbuff"
}
set test { "" "this is a test\n" "this is a testttt\n" "thirst" "t"
"this is a test\n\n\n" "this is a tes\nt\n\n" }
set testnum 0
foreach tstr $test {
puts " ignore newline and t \"$tstr\" =>\"[chop $tstr { "\n"
"t" } ]\""
puts " ignore newline and t truncated \"$tstr\" =>\"[chop $tstr
{ "\n" "t" } 1 ]\""
puts " ignore nothing \"$tstr\" =>\"[chop $tstr {} ]\" "
puts " ignore nothing truncated \"$tstr\" =>\"[chop $tstr {} 1 ]
\" "
}
HTH Carl
proc chop {s {ignorechars {}} {truncate 0}} {
set tmp [string trimright $s $ignorechars]
set idx [string length $tmp]
set postbuff [string range $s $idx end]
set tmp [string range $tmp 0 end-1]
if {!$truncate} then {
append tmp $postbuff
}
return $tmp
}
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
No. Perl's chop removes the last character, period. [string trim] and
friends remove (by default) all trailing whitespace. The [string range]
and [string replace] solutions are correct.
--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan
Are we sure it's really the last character, or is it the last byte?
Yes, it is the last character, not byte. Run:
my $a = "\x{0061}\x{00e9}";
print $a;
chop($a);
print $a;
The result is a - lower case e with acute accent - a,
showing that the chop removes the accented e, which is two bytes.