Jump to content

Replace 2 or more characters from a string


Recommended Posts

Good morning :)
I was trying to replace 2 identical characters from a string, but I didn't manage to with StringReplace()...
Does anyone know how to replace two ( i.e. : """" with ";" ) ? 
Thanks :) 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

are there other cases, or just if you see four quotes change the middle two?  Or is that just two quotes inside quotes around the string?

anyway, the literal answer with the most edge cases:

$string = '""""'

msgbox(0, '' , stringleft($string , 1) & stringreplace(stringtrimleft(StringTrimRight($string , 1) , 1) , '""' , ";") & stringright($string , 1))

 

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

Link to comment
Share on other sites

Maybe this works? 

Local $sOriginal = "1ixxj00u!!_((sometthinng"
Local $sFinal = ""

For $i = 1 To StringLen($sOriginal)
    $sOne = StringMid($sOriginal, $i, 1)
    $sTwo = StringMid($sOriginal, $i+1, 1)
    If $sOne = $sTwo Then
        $sFinal = $sFinal & ";"
        $i = $i + 1
    Else
        $sFinal = $sFinal & $sOne
    EndIf
Next

ConsoleWrite("Final: " & $sFinal & @CRLF)

 

TY.

Link to comment
Share on other sites

Hey guys!
Thank you for the replies!
I have a string ( read from a file ) which has this format: 
 

5   "2017-03-24 09:21:16"   "-1:00" "System"    "Application"   "Change to operating mode 'online'."        4opute

When I read the file, the first operation I do with this string is:
 

$aFileAudit_TXT_Content[$i] = StringStripWS($aFileAudit_TXT_Content[$i], $STR_STRIPSPACES)

So, I have the string without all those spaces...
Then, I'd like to have something like a CSV file format, so, the string after StringStripWS would be like:
 

5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode 'online'."4opute

Do you guys see that there are too many double quotes, and I'd like to have just this kind of format:
 

5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

So, I can have something more familiar compared to a CSV.

Practically, I need to replace 2 double quotes ( in code, """"), with just one ; 
Hope I've been clear as possible :) 
Thanks for your help!

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

7 minutes ago, FrancescoDiMuro said:

I'd like to have just this kind of format:
 

5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute
Local $sOriginal = '"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'
Local $sFinal = ""

For $i = 1 To StringLen($sOriginal)
    $sOne = StringMid($sOriginal, $i, 1)
    $sTwo = StringMid($sOriginal, $i+1, 1)
    If $sOne = '"' And $sTwo = '"' Then
        $sFinal = $sFinal & '"'
        $i = $i + 1
    Else
        $sFinal = $sFinal & $sOne
    EndIf
Next

;finally replace " with ;
$sFinal = StringReplace($sFinal, '"', ';')
ConsoleWrite("Final: " & $sFinal & @CRLF)
;The result is --> ;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

 

If you don't want to replace " with ; then comment out the string replace line

a

TY.

Link to comment
Share on other sites

Local $string = '"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'

msgbox(0, '' , stringreplace(stringreplace(stringreplace($string , '""' , ";") , "''" , "'"), '"' , ""))

 

Edited by iamtheky

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

Link to comment
Share on other sites

7 minutes ago, iamtheky said:
Local $string = '"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'

msgbox(0, '' , stringreplace(stringreplace(stringreplace($string , '""' , ";") , "''" , "'"), '"' , ""))

 

I get all "", all ; and all spaces removed...
That's not what I was talking about... 
Maybe because the string you used it's not like the string I've posted... :/

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

18 minutes ago, taylansan said:
Local $sOriginal = '"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'
Local $sFinal = ""

For $i = 1 To StringLen($sOriginal)
    $sOne = StringMid($sOriginal, $i, 1)
    $sTwo = StringMid($sOriginal, $i+1, 1)
    If $sOne = '"' And $sTwo = '"' Then
        $sFinal = $sFinal & '"'
        $i = $i + 1
    Else
        $sFinal = $sFinal & $sOne
    EndIf
Next

;finally replace " with ;
$sFinal = StringReplace($sFinal, '"', ';')
ConsoleWrite("Final: " & $sFinal & @CRLF)
;The result is --> ;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

 

If you don't want to replace " with ; then comment out the string replace line

a

Same for you buddy... What I want to replace is the double quotes with a single ;
 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

You need a RegEx:

Local $sString = '5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'

$sNewString = StringRegExpReplace($sString, '\x22{1,2}', ";")

ConsoleWrite($sNewString & @CRLF)
ConsoleWrite("5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute" & @CRLF)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hey @Melba23
I was trying your code, but I have always the same result!

This is the line I read from the file:

5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode 'online'."4opute

And this is what I'd like to have:

5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'."4opute

I can then do a StringReplace(5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'."4opute, '"', ";") and have finally:

5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

But, with all your scripts, I can't manage to have my wanted result...
This is my full script:
 

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <StringConstants.au3>


Global $sCartellaAudit_TXT = @ScriptDir & "\Audit_TXT\"

Global $aFileAudit_TXT = _FileListToArray($sCartellaAudit_TXT, "*.txt", $FLTA_FILES)

Global $sFileAudit_TXT = $aFileAudit_TXT[1]

Local $hFileAudit_TXT = FileOpen($sCartellaAudit_TXT & $sFileAudit_TXT, $FO_READ)

Local $sFinal

If($hFileAudit_TXT <> -1) Then
    Local $aFileAudit_TXT_Content
    _FileReadToArray($sCartellaAudit_TXT & $sFileAudit_TXT, $aFileAudit_TXT_Content)
    If(IsArray($aFileAudit_TXT_Content)) Then
        For $i = 0 To UBound($aFileAudit_TXT_Content) - 1
            $aFileAudit_TXT_Content[$i] = StringStripWS($aFileAudit_TXT_Content[$i], $STR_STRIPSPACES)
        Next
        For $i = 0 To UBound($aFileAudit_TXT_Content) - 1
            $aFileAudit_TXT_Content[$i] = StringRegExpReplace($aFileAudit_TXT_Content[$i], '\x22{1,2}', ";")
        Next
        _ArrayDisplay($aFileAudit_TXT_Content, "Array:")
    Else
        MsgBox($MB_ICONERROR, "Errore!", "$aFileAudit_TXT_Content non è un array!" & @CRLF & "Errore: " & @error)
    EndIf
Else
    MsgBox($MB_ICONERROR, "Errore!", "Errore durante l'apertura del file:" & @CRLF & $sFileAudit_TXT & @CRLF & "Errore: " & @error)
EndIf

I divided the two For just to try if I had to do first a StringStripWS and then a StringRegExpReplace... But nothing changed...

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

this?

Local $string = '5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'

msgbox(0, '' , stringreplace(stringreplace(stringreplace($string , '""' , ";") , "''" , "'"), '"' , ";"))

 

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

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

It works for me - once you get the correct formatting for the string:

Local $sString = '5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode ''online''."4opute'

$sNewString = StringRegExpReplace($sString, '\x22{1,2}', ";")

ConsoleWrite("Original string:" & @CRLF & _
                $sString & @CRLF & _
                "New string:" & @CRLF & _
                $sNewString & @CRLF & _
                "Compared to what you required" & @CRLF & _
                "5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute" & @CRLF)

gives me:

Original string:
5"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode 'online'."4opute
New string:
5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute
Compared to what you required
5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

M23

Edited by Melba23
Wrong format for second code tag

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23
This is copied from the _ArrayDisplay...
I get that array element and pass it to StringRegExpReplace...

Row|Col 0
[3]|5    "2017-03-24 09:21:16"    "-1:00"    "System"    "Application"    "Change to operating mode 'online'."    4opute


This is my result:

Row|Col 0
[3]|5    ;2017-03-24 09:21:16;    ;-1:00;    ;System;    ;Application;    ;Change to operating mode 'online'.;    4opute

Can you please tell me why? :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

You said earlier:

Quote

the first operation I do with this string is: [...] StringStripWS

And so the code I posted works on the already stripped string - as a simple glance at either the code or result I posted above clearly shows.

Running the RegEx without removing the spaces will just replace single " - as is shown by your result. Stripping the spaces will remove both " and "" - which was your original request.

So take your string, strip the spaces and then run the RegEx - then you will get the result you wish.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry for posting "wrong" examples...
Seems that _ArrayDisplay add it's own spaces to the rows...

As I've already told you, when I read the file through _FileReadToArray(), then, I manage the array returned by _FileReadToArray(), and, the first thing I do with the array, is StringStripWS through every row of the array, in order to remove the spaces ( 2 or more with the parameter $STR_STRIPSPACES ), and then I do the StringRegExpReplace as you suggested to me... But the result I obtain IS what I've posted above and I'll post again here:

This is what I LITERALLY read from the array:

Quote

"2017-03-24 09:21:16""-1:00""System""Application""Change to operating mode 'online'." 4opute

This is what I LITERALLY obtain from the StringStripWS() before and then StringRegExpReplace():

Quote

;2017-03-24 09:21:16;;-1:00;;System;;Application;;Change to operating mode 'online'.;4opute
 

This is the code I'm working on:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <StringConstants.au3>


Global $sCartellaAudit_TXT = @ScriptDir & "\Audit_TXT\"

Global $aFileAudit_TXT = _FileListToArray($sCartellaAudit_TXT, "*.txt", $FLTA_FILES)

Global $sFileAudit_TXT = $aFileAudit_TXT[1]

Local $hFileAudit_TXT = FileOpen($sCartellaAudit_TXT & $sFileAudit_TXT, $FO_READ)

Local $sFinal

If($hFileAudit_TXT <> -1) Then
    Local $aFileAudit_TXT_Content
    _FileReadToArray($sCartellaAudit_TXT & $sFileAudit_TXT, $aFileAudit_TXT_Content)
    If(IsArray($aFileAudit_TXT_Content)) Then
        For $i = 0 To UBound($aFileAudit_TXT_Content) - 1
            $aFileAudit_TXT_Content[$i] = StringStripWS($aFileAudit_TXT_Content[$i], $STR_STRIPSPACES)
            $aFileAudit_TXT_Content[$i] = StringRegExpReplace($aFileAudit_TXT_Content[$i], '\x22{1,2}', ";")
        Next
        _ArrayDisplay($aFileAudit_TXT_Content, "Array:")
    Else
        MsgBox($MB_ICONERROR, "Errore!", "$aFileAudit_TXT_Content non è un array!" & @CRLF & "Errore: " & @error)
    EndIf
Else
    MsgBox($MB_ICONERROR, "Errore!", "Errore durante l'apertura del file:" & @CRLF & $sFileAudit_TXT & @CRLF & "Errore: " & @error)
EndIf

Try it yourself since I can't be understood through my words...

AuditTrailDosaggio0_20170324_131507_DOSAGGIO_PW_01.txt

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

It is the  StringStripWS command that is the problem - it is not returning the stripped string in the format you believe it should. I am looking into it.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

First an explanation. You were using $STR_STRIPSPACES with StringStripWS - this removes all multiple spaces between the words, which is required because you want to keep the spacing of the "Change to operating mode" section. However, doing this did not remove the single space between these elements ("-1:00" "System") nor did it remove the @TABs that form the first and final whitespace elements of the line. So the stripped string did not match what you believed you were getting - which is why you did not get the results expected when you ran the proposed solutions. You were not providing us with the actual input that you were using - a good case of GIGO

The solution is a RegEx that removes all of the unwanted whitespace (both space and tab) while retaining the spacing within the  "Change to operating mode" section:

$sString = '5   "2017-03-24 09:21:16"   "-1:00" "System"    "Application"   "Change to operating mode ''online''."      4opute'

$sStrippedString = StringRegExpReplace($sString, '("\h+"|\h+"|"\h+)', ";")

ConsoleWrite($sString & @CRLF)
ConsoleWrite($sStrippedString & @CRLF)

It looks for any horizontal whitespace (i.e. both space and tab) preceded, followed, or surrounded by a " - this way it leaves the internal spaces - here is the result I get:

5   "2017-03-24 09:21:16"   "-1:00" "System"    "Application"   "Change to operating mode 'online'."        4opute
5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute

So use this RegEx in place of both the StringStripWS & StringRegExpReplace lines you were previously using and you should get what you require - I certainly do when I run it on the file you posted:

For $i = 1 To $aFileAudit_TXT_Content[0]
    $aFileAudit_TXT_Content[$i] = StringRegExpReplace($aFileAudit_TXT_Content[$i], '("\h+"|\h+"|"\h+)', ";")
Next
5;2017-03-24 09:21:16;-1:00;System;Application;Change to operating mode 'online'.;4opute
6;2017-03-24 09:21:17;-1:00;System;User administration;User administration imported successfully.;y7Ugni
7;2017-03-24 09:21:49;-1:00;Admin;User administration;User 'Admin' logged on with group 'Amministratore_DOS_PW'.;YB/XAZ
8;2017-03-24 09:21:49;-1:00;Admin;Application;Shutting down application.;PnTovK

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

×
×
  • Create New...