@SUBST ... how replace character with a caret?

May 20, 2008
11,538
103
Syracuse, NY, USA
I want a single character to be replaced by a '^'. How? This didn't work:

Code:
v:\> echo %@subst[2,^^,foobar]
fo^ar
 
May 20, 2008
11,538
103
Syracuse, NY, USA
This one behaves rather oddly:

Code:
v:\> echo %@subst[3,%%@char[94],foobar]
foo^

What happened to the rest of the string?
 
Nov 2, 2008
237
2
What seems to be happening is that subst is seeing the string ^^ or ^& as two characters, eg subst[2,^^,foobar] gives fo^ar, while subst[2,^&,foobar] gives fo&ar. This comes by that subst[2,^&,foobar] is actually returning fo^&ar, and this is later expanded to fo&ar.

This works around it. You can use any character after the first /E, that is not likely to appear in your headings. %= accesses the escape character, and a second setdos will restore it to its default. For some generic batch, you could get away with an environment variable.

setdos /E£ & echo %@subst[2,^,foobar & setdos /E^

gives fo^bar

If you don't want to use the second setdos, you could nest the command in a setlocal / endlocal.
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
This one behaves rather oddly:

Code:
v:\> echo %@subst[3,%%@char[94],foobar]
foo^

What happened to the rest of the string?

It was overwritten, as you specified.

@SUBST is an overwrite, not an insertion. What you passed to @SUBST was "3,%@char[94],foobar". So this translates to :

Write the string "%@char[94]" to the string "foobar", starting at position 3. The result is "foo%@char[94]".
 
Vefatica wrote:


>I want a single character to be replaced by a '^'. How? This didn't work:
>
>
> Code:
> ---------
> v:\> echo %@subst[2,^^,foobar]
> fo^ar

Charles Dye's SafeChars plugin makes it really easy:

C:\>echo %@subst[3,%_caret,foobar]
foo^ar

--
Howard
 
May 20, 2008
11,538
103
Syracuse, NY, USA
On Sun, 27 Feb 2011 09:53:05 -0500, rconn <> wrote:

|SETDOS /X-8 is your friend.

Yeah! It has been 20 years since I started with 4DOS and it never fails ...
just when I **need** SETDOS, I have forgotten about it. Thanks for the
reminder.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
---- Original Message ----
From: rconn
To: ESFabian@zenge.org
Sent: Sunday, 2011. February 27. 09:39
Subject: RE: [Support-t-2635] Re: @SUBST ... how replace character with
a caret?

| Quote:
| Originally Posted by vefatica
| This one behaves rather oddly:
|
|
| Code:
| v:\> echo %@subst[3,%%@char[94],foobar]
| foo^What happened to the rest of the string?
|
| It was overwritten, as you specified.
|
| @SUBST is an overwrite, not an insertion. What you passed to @SUBST
| was "3,%@char[94],foobar". So this translates to :
|
| Write the string "%@char[94]" to the string "foobar", starting at
| position 3. The result is foo%@char[94].

@SUBST[n, string1, string2]: Substitutes string1 starting at position n in string2.

F:\JPSOFT>*for /l %n in (0,1,9) echo %n %@subst[%n,x,0123456]
0 x123456
1 0x23456
2 01x3456
3 012x456
4 0123x56
5 01234x6
6 012345x
7 0123456x
8 0123456x
9 0123456x

Using "xy" as the replacement string has similar result - 2 consecutive characters are replaced with "xy". Based on the above result, I'd like a more explicit explanation of "boundary conditions" (i.e., when position n is not part of the string). And I still do not see why replacing a single character in the string with the caret ^ truncates the string?
--
Steve
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
Using "xy" as the replacement string has similar result - 2 consecutive characters are replaced with "xy".

WAD.

Based on the above result, I'd like a more explicit explanation of "boundary conditions" (i.e., when position n is not part of the string).

It's appended to the end of the string. But the user bears some responsibility to not pass garbage!

And I still do not see why replacing a single character in the string with the caret ^ truncates the string?

It never does -- Vincent wasn't replacing a single character, he was overwriting the string with the literal string "%@char[94]" -- NOT the result of the @char.
 
Similar threads
Thread starter Title Forum Replies Date
D SUBST Support 8
vefatica DEL /R on a SUBST'd drive? Support 1
Joe Caverly Documentation Replace internal command with LIBRARY command Support 0
I how to make TCC default/replace cmd.exe? Support 9
x13 TPIPE /REPLACE - Remove line (including EOL) Support 3
T @replace -- what is wrong here Support 2
C Replace 1st argument on command line Support 4
R How to? Replace in Rename Support 9
C How to? @replace[a comma,_,filename] Support 5
MickeyF Can't get my TPIPE /replace to work Support 4
C @replace and @xreplace problems Support 5
C How to? Replace &'s in filenames / FixNames.dll Support 2
M I'm unable to use @replace[] with a % character in string2 Support 12
MikeBaas Strange prob with %@replace.. Support 4
vefatica Back-refs in TPIPE's /replace's replacement string? Support 2
Frank @replace works case sensitive Support 5
J @replace weirdness Support 1
vefatica TPIPE /replace? Support 3
Stefano Piccardi replace tcc process space? Support 8
mdwyer @replace - global use in a text file Support 3
B Regex and Replace Support 6
B REPLACE is good news Support 0
J Problem with %var:find=replace% syntax Support 5
vefatica WAD TCC: inconsistent character handling Support 11
vefatica @INT with comma as decimal character Support 2
vefatica What's this character? Support 12
rps Regex problem: \xnn not recognized as a hex character Support 0
old coot Regex problem: \xnn not recognized as a hex character Support 12
L How to? Character transfer among @BALLOC buffers, command tails, and possibly redirected standard streams Support 11
D How to? Filenames with "&" character Support 3
samintz WAD Copy/append adds a character Support 3
B Fixed FFIND doesn't find last character in file without CR/LF at EOF Support 1
Exolon V17 beta, debugger: STRG-S is inserting character instead of saving Support 12
J Passing % character through command line to .btm Support 5
M Fixed character set in dir/copy Support 3
dcantor Fixed Ctrl+Shift+Insert repeats last character Support 3
D Copy to clip changes character Support 24
S Fixed Build 48 - Non-ASCII Character mishandling Support 3
W Ascii character output Support 12
nickles Literal tab character Support 21
R One character alias prevents completion? Support 8
S Forum character encoding Support 2
M Escape-character issue... Support 7
M Programmatically getting the console width in character Support 18
J How to pass a character like " " to a function Support 3
Jay Sage Dealing With Filenames Containing Slash Character Support 5
B A swedish character sent through keystack gets converted Support 4
nikbackm Alias expansion and switch character Support 2

Similar threads