Jump to content
Sign in to follow this  
anixon

String Replace

Recommended Posts

anixon

Have been unable to get my mind around this one even after studying string processing in Help. If a string contains CRLF CR and/or multiple spaces how do you replace these non typing characters with a single space.

For example what code would you use to change this text string:

Before :

'This is an example

of the text

to be fixed'

After:

'This is an example of the text to be fixed'

Help is always appreciated

Share this post


Link to post
Share on other sites
FireFox

@anixon

$delCRLF = StringReplace($s_string, @CRLF, ' ')
$delCR = StringReplace($delCRLF, @CR, ' ')
MsgBox(64, 'StringReplace', $delCR)

Not tested,

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
anixon

@anixon

$delCRLF = StringReplace($s_string, @CRLF, ' ')
$delCR = StringReplace($delCRLF, @CR, ' ')
MsgBox(64, 'StringReplace', $delCR)

Not tested,

Cheers, FireFox.

Thanks for that works with cr and crlf what about if the text contains multiple spacing like test.....and this was..........the result. Please read the periods 'full stops' as spaces Ant.. Edited by anixon

Share this post


Link to post
Share on other sites
Spiff59

Or maybe:

$str = @CR & "  This    is a   test  " & @CR & @CRLF & "   .  "
$str = StringStripWS($str ,7)
Msgbox(1,"",$str)

EDIT: Oops, typo

Edited by Spiff59

Share this post


Link to post
Share on other sites
anixon

This is a combination of both codes that does the requested task. Thanks for your help.

CODE
$s_string = "This is test" & @crlf & "to see whats what" & @crlf & "whats what again"

$delCRLF = StringReplace($s_string, @CRLF, ' ')

$delCR = StringReplace($delCRLF, @CR, ' ')

$str = StringStripWS($delCR ,7)

Msgbox(1,"",$str)

Share this post


Link to post
Share on other sites
Spiff59

This is a combination of both codes that does the requested task. Thanks for your help.

CODE
$s_string = "This is test" & @crlf & "to see whats what" & @crlf & "whats what again"

$delCRLF = StringReplace($s_string, @CRLF, ' ')

$delCR = StringReplace($delCRLF, @CR, ' ')

$str = StringStripWS($delCR ,7)

Msgbox(1,"",$str)

If you read the help file on StingStripWS, it by itself removes:

Chr(9) thru Chr(13) which are HorizontalTab, LineFeed, VerticalTab, FormFeed, CarriageReturn, the null string "" (Chr(0)) and the standard space " " (Chr(32))

Share this post


Link to post
Share on other sites
BrettF

$s_string = "This is test" & @crlf & "to see whats what" & @crlf & "whats what again"
$str = StringRegExpReplace ($s_string, "\s+", " ")
Msgbox(1,"",$str)

I'm getting better at this RegExp stuff me thinks! :)

Share this post


Link to post
Share on other sites
anixon

$s_string = "This is test" & @crlf & "to see whats what" & @crlf & "whats what again"
$str = StringRegExpReplace ($s_string, "\s+", " ")
Msgbox(1,"",$str)

I'm getting better at this RegExp stuff me thinks! :)

You can say that again reducing a number of steps into a single line of code I am humbled by the experience Ant..

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.