Jump to content

Javascript Substr equivalent


Go to solution Solved by Andreu,

Recommended Posts

Hi,

I would like to extract a portion of a string:

JS: "Hello world A n Hello Universe B n Hello Earth Cn"

AutoIt: "Hello world A {@CRLF} Hello Universe B {@CRLF} Hello Earth C {@CRLF}"

If i pass "Universe" to the function, I would like to get "B".

Javascript Code:

var str="Hello world A n Hello Universe B n Hello Earth Cn";
var param="Universe"
var pos = str.indexOf(param) +param.length ;
var n= str.substr(pos);
pos = n.indexOf("n");
n = n.substr(0,pos);
 

<<n would return B>>

AutoIt code:

$str="Hello world A {@CRLF} Hello Universe B {@CRLF} Hello Earth C {@CRLF}"
$param="Universe"
$pos= StringInStr ($str, $param) + StringLen($param)
;todo
$pos2 = StringInStr($str, @CRLF) 
;todo

What would be a good equivalent here?

Thank You!

 

Link to comment
Share on other sites

  • Solution

This is written assuming no prior knowledge of AutoIt. If you aren't new, then... Simply, your answer is StringMid(). (Though, if your data is that consistent, looking into StringRegExp() would suit your needs more effectively, judging by what I assume you plan "todo" next. ;) )

Example of Newbie way to go about it:

$str="Hello world A {@CRLF} Hello Universe B {@CRLF} Hello Earth C {@CRLF}"; Original String
$param="Universe"; Substring
$pos = StringInStr($str, $param) + StringLen($param) + 1; Where the substring starts
$end = StringInStr($str, " ", "", 1, $pos); The first space after the substring
$Final = StringMid($str, $pos, $end - $pos); Extracts it from the middle
MsgBox (0, "Done", "Result Text: " & $Final); Yea...

You can make this a whole lot shorter, condensing it down to just a one liner... But this is intended to be easier to read.

Edit:

Just noticed in your post you stated you wanted to pass "Universe" to a function... Here. 

(This one will let you put Earth, World, Universe... etc.)

 

$str = "Hello world A {@CRLF} Hello Universe B {@CRLF} Hello Earth C {@CRLF}"; Original String
MsgBox (0, "Search Results", _Search(InputBox("Search Prompt", "What would you like to search for?", "Universe"))); Rcv the input, pass it to the function, display result

Func _Search($param)
   $pos = StringInStr($str, $param) + StringLen($param) + 1; Where the substring starts
   $end = StringInStr($str, " ", "", 1, StringInStr($str, $param) + StringLen($param) + 1); The first space after the substring
   Return StringMid($str, $pos, $end - $pos); Extracts it from the middle
EndFunc
Edited by Andreu
Link to comment
Share on other sites

Overkill, but just to demonstrate regexp

#include <Array.au3>
$variable = "Universe"
$str="Hello world A" & @CRLF & "Hello Universe B" & @CRLF & "Hello Earth C" & @CRLF
ConsoleWrite($str)
$array = StringRegExp($str,$variable & "\s(\w+)",3)
For $i = 0 To UBound($array)-1
    ConsoleWrite("Match=[" & $i+1 & "]; value=[" & $array[$i] & "]" & @CRLF )
Next
_ArrayDisplay($array)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Thank you both!

@Andreu - StringMid is exactly what i was looking for - infact i did notice it when i was skimming through the list of functions related to StringInStr - but i ignored it thinking it probably returns the mid of a string or something haha - its kinda a misnomer, but its all good :)

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...