Jump to content

Replace string


Recommended Posts

I have a string which looks like this:

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75

I want to subtract 0.50 to 1012.75 giving me 1012.25.

Is this possible to do in a calculation string?

:mellow:

Above is just an example, the numbers can be others and change often.

Edited by jorgeng
Link to comment
Share on other sites

Here is one example.

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$a=StringRegExp($string,"kurs (\S*)",1)
$b = $a[0] - 0.5
ConsoleWrite($b & @LF)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Extract that part of the string with StringMid() or other string manipulation, or maybe StringRegExp(). Do the math, then put the result back in the string.

If you get stuck, post what you tried for more help.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

And another simple way

$sStr = '15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'

$iNewValue = StringRegExpReplace($sStr, "^.+\h([\d.]+)$", "$1") -.5
If @Extended Then
    MsgBox(0, "Result", $iNewValue)
Else
    MsgBox(0, "Error", "Invalid string entered")
EndIf

Edit: Added code to check for a valid string.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks everybody.

They all works, but i want to have the result replaced in original string, how to do that?

Now the string looks like:

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75

and i want

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.25

How to do that?

:mellow:

Link to comment
Share on other sites

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$a=StringRegExp($string,"kurs (\S*)",1)
$b = $a[0] - 0.5
ConsoleWrite($b & @LF)
$c = StringReplace($string,$a[0],$b)
ConsoleWrite($c & @LF)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

$sStr = '15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.50'
$aStr = StringRegExp($sStr, "^(.+\h)([\d.]+)$", 1)
If IsArray($aStr) Then
    $aStr[1] = StringFormat( "%.2f", $aStr[1] -.5)
    $sStr = $aStr[0] & $aStr[1]
    MsgBox(0, "Result", $sStr)
Else
    MsgBox(0, "Error", "Invalid string entered")
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Or

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$a=StringRegExp($string,"kurs (\S*)",1)
$b = $a[0] - .5
If Not StringInStr($b,".") Then $b &= ".00"
$c = StringReplace($string,$a[0],$b)
ConsoleWrite($c & @LF)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...