AutoHotkey Community

It is currently May 27th, 2012, 12:37 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: StringInsert
PostPosted: September 11th, 2005, 8:48 pm 
Is there missing a command like StringInsert?

e.g. StringInsert, OutputVar, InputVar, InsertText, Position
I'm looking for s.th like this.

A search in the forum revealed this function:

Code:
StringInsert(In_String,In_Posi,In_Insert)
{
  local Length, Before, After, Ret_Val
  StringLen, Length, In_String
  if (In_Posi>Length)or(length=0)or(In_Posi<0)or(In_Insert=)
     return, %In_String%
  StringLeft, Before, In_String, In_Posi
  StringRight, After, In_String, Length-In_Posi
  Ret_Val=%Before%%In_Insert%%After%   
  return, %Ret_Val%
}


But I've got some question concerning it.
Is position counted with the first char being "1" like in "StringMid" or "0" like in "StringGetPos"?
And what about the input. Are they expressions? Do I have to write "var" or "%var%" for e.g. In_String. And what about the other two.

Sorry if this sound stupid. ;) But I don't know it at the moment.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 7:24 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Hi AGU,

It is a function, so, when you call it you specify the var without % or a string with "". E.g.
Code:
MyName := StringInsert("tolf",2,"ra")
or
Code:
var1 = tolf
MyName := StringInsert(var1,2,"ra")
or
Code:
var1 = tolf
var2 = ra
MyName := StringInsert(var1,2,var2)
I assume that you can use numbers without "" (I haven't tested) but to be sure you could put them in "".

Regarding the position. You have to look at the command StringLeft / StringRight
Quote:
Count = The number of characters to extract
So the position to insert text after the first character is 1.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 7:36 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
BTW. there is a bug in the function, this should be right:
Code:
StringInsert(In_String,In_Posi,In_Insert)
{
  local Length, Before, After, Ret_Val
  StringLen, Length, In_String
  if (In_Posi>Length)or(length=0)or(In_Posi<0)or(In_Insert=)
     return, In_String
  StringLeft, Before, In_String, In_Posi
  StringRight, After, In_String, Length-In_Posi
  Ret_Val=%Before%%In_Insert%%After%   
  return Ret_Val
}
The return value has to be without %%.


If you also remove the condition that if the insert string is empty, because it is a trivial solution. And handle the other conditions fail save the function could look like this
Code:
StringInsert(In_String,In_Posi,In_Insert)
{
  StringLen, Length, In_String
  if ( In_Posi > Length )
     return In_String . In_Insert
  if ( Length = 0 or In_Posi < 0)
     return In_Insert . In_String
  StringLeft, Before, In_String, In_Posi
  StringRight, After, In_String, Length-In_Posi
  return Before . In_Insert . After
}

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 7:47 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
toralf wrote:
The return value has to be without %%.


Hmm...
Code:
MsgBox, % test("This is a test without using the % signs")
MsgBox, % test2("This is a test with using the % signs")

test(a)
{
a = %a% -> Ok
Return, a
}

test2(a)
{
a := a " -> Ok"
Return, %a%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 7:57 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
You are write, you example works for both cases. I wasn't testing it. Taken from the manual
Quote:
Introduction and Simple Example
A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller. In addition, a function may optionally return a value to its caller. Consider the following simple function which accepts two numbers and returns their sum:

Add(x, y)
{
return x + y ; "Return" expects an expression.
}
I assumed that the return value is handeled as an expression and therefor %% would lead to an error. But Chris seamed to have put some AI into this. :)

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 8:03 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Consider this
Code:
MsgBox, % test1("This is a test without using the % signs")
MsgBox, % test2("This is a test without using the % signs")
MsgBox, % test3("This is a test with using the % signs")

test1(a)
{
va =  -> Ok 1
Return, a . va
}

test2(a)
{
Return, a . " -> Ok 2"
}

test3(a)
{
Return, %a% -> Error 3
}

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 8:34 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
Good Point :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 1:08 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I think "return %a%" works only because there is legacy code that allows things like "Sleep a" and "Sleep %a%" to work the same. In a way, this is bad because it prevent's return's expression from being a double reference. For example, if x contains the string "y", "return %x%" should return the contents of the variable y. But instead it returns the contents of the variable x.

Although fixing this would break existing scripts that rely on this behavior, it would make Return more consistent with the documentation of expressions. I wonder which is best. I think I'm leaning toward leaving it the way it is, perhaps adding documentation to explain it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 6:49 pm 
Thx for all the detailed help. :)

Another question:
I'm looking for a StringReplace Command that lets me replace a substring by passing a start position and and end position to the Input String.

Know what I mean?

And what about the StringInsert command in AHK Style? Is there planned s.th like this?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 8:39 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
There's no plan for a built-in StringInsert, though there might be a #include'able function someday distributed as part of a standard library.

As for your StringReplace question, I think you would have to script it yourself using something like StringMid+StringReplace.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group