IanN1990

Stringreplace behaviour

10 posts in this topic

#1 ·  Posted

Hi, 

This topic is improve my understanding of stringreplace:)

StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] )

Searchstring/start = The substring to search for or the character position to start the replacement.

$ExampleText = "123456789"
$Replacedtext = StringReplace($ExampleText, 4, "")
ConsoleWrite($Replacedtext & @crlf)

The example fails to remove the fourth character and i dont understand why.

Share this post


Link to post
Share on other sites



#2 ·  Posted

As the help says:

" If the start method is used the occurrence and casesense parameters are ignored. The function will replace the characters in "string", starting at the requested position, with the characters in "replacestring" - as many characters will be replaced as are in "replacestring". However, if there are not enough characters in "string" for the entire "replacestring" to be inserted an empty string is returned and @error is set to 1. "

In your example, "replacestring" has length zero, hence zero characters are replaced.

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

you have to use strings, not numbers, try with replacestring as "4" instead of the number 4:

$ExampleText = "123456789"
$Replacedtext = StringReplace($ExampleText, "4", "")
ConsoleWrite($Replacedtext & @crlf)

edit:

P.S. jchd correctly assumes you want to use the second parameter as "start point" method, as it seems from your example, but I think that you just want replace the substring "4" with nothing (just removing it)

Edited by Chimp
1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#4 ·  Posted

3 hours ago, jchd said:

As the help says:

" If the start method is used the occurrence and casesense parameters are ignored. The function will replace the characters in "string", starting at the requested position, with the characters in "replacestring" - as many characters will be replaced as are in "replacestring". However, if there are not enough characters in "string" for the entire "replacestring" to be inserted an empty string is returned and @error is set to 1. "

In your example, "replacestring" has length zero, hence zero characters are replaced.

I understand the logic. So there is no way to remove a single character using StringReplace and the character location?

Below is a proof of concept.

$ExampleText = "123456789"
$Replacedtext = StringReplaceBlank($ExampleText, 4)
ConsoleWrite($Replacedtext & @crlf)

Func StringReplaceBlank($String, $Number)
    $NewString = StringLeft($String, $Number-1)
    $NewString &= StringRight($String, StringLen($String)-$Number)
    Return $NewString
EndFunc

 

1 hour ago, Chimp said:

you have to use strings, not numbers, try with replacestring as "4" instead of the number 4:

$ExampleText = "123456789"
$Replacedtext = StringReplace($ExampleText, "4", "")
ConsoleWrite($Replacedtext & @crlf)

edit:

P.S. jchd correctly assumes you want to use the second parameter as "start point" method, as it seems from your example, but I think that you just want replace the substring "4" with nothing (just removing it)

Jcdh assumption was correct. I was looking to remove any character at requested location 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Try this:

$ExampleText = "123456789"
$Replacedtext = StringRegExpReplace($ExampleText, "(?<=^...)(.)", "")
ConsoleWrite($Replacedtext & @crlf)

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#6 ·  Posted

$ExampleText = "123456789"

$PositionToRemove = 4

$Replacedtext = StringReplace($ExampleText, stringmid($ExampleText , $PositionToRemove , 1) , "")
ConsoleWrite($Replacedtext & @crlf)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

A small improvement on the @jchd's pattern lets you choose the starting point and how many chars to remove

$ExampleText = "123456789"
ConsoleWrite(StringRemoveChars($ExampleText, 4) & @CRLF)

; This function removes chars from a string
; $String   The string to elaborate
; $iStart   The starting char position
; $Number   [optional] number of chars to remove (default 1)
;
Func StringRemoveChars($String, $iStart, $Number = 1)

    Return StringRegExpReplace($String, "(?<=^.{" & $iStart - 1 & "})(.{" & $Number & "})", "")

EndFunc   ;==>StringRemoveChars

 

5 hours ago, iamtheky said:
$ExampleText = "123456789"

$PositionToRemove = 4

$Replacedtext = StringReplace($ExampleText, stringmid($ExampleText , $PositionToRemove , 1) , "")
ConsoleWrite($Replacedtext & @crlf)

 

..... Hi @iamtheky,

try to remove with your function only the fourth char from this string "4444444" .....

Edited by Chimp
syntax correction

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#8 ·  Posted

$ExampleText = "4444444"

$PositionToRemove = 4

$Replacedtext = stringright($ExampleText , $PositionToRemove - 1) & stringmid($ExampleText , $PositionToRemove + 1)
ConsoleWrite($Replacedtext & @crlf)

yeah I was dumb, this should be better/


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Not much of a tutorial on StringReplace(), but try:

$x = StringRemoveChars("Mississippi", 4, 3)
Exit MsgBox(0, "StringRemoveChars", $x)

Func StringRemoveChars($sStringIn, $iStartPos, $iCount = 1)
    Return StringLeft($sStringIn, $iStartPos - 1) & StringMid($sStringIn, $iStartPos + $iCount)
EndFunc   ;==>StringRemoveChars

;)

Edited by CarlD
Working example of code

Share this post


Link to post
Share on other sites

#10 ·  Posted

My 2 cents  :idiot:

$ExampleText = "123456789"
$PositionToRemove = 4
$Replacedtext = StringRegExpReplace($ExampleText, '^.{' & $PositionToRemove-1 & '}\K.', "")
ConsoleWrite($Replacedtext & @crlf)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now