Sign in to follow this  
Followers 0
Rex

Place chr at xx position in string

9 posts in this topic

Hi

Been looking all over the forum and in helpfile, but cant find any way to place a char / string in a known position in a string.

At work we use a template that looks some things like this

-------------

|              |

|   Data   |

|             |

------------

| Other   |

------------

And we updates the "Data" and "other" with new info, but the txt is not alway the same lenth, so we have to either add or remove white spaces to keep the format.

But some times one forgets to update/change the data, so i wanned to create a simple gui to type in the data, and then creates a new file with the correct layout, but I hit the wall not knowing how to place the most right char at the correct position, searching the forum hoping to find a snipet that could do place X at XX in string, but found nothing.

Is there some one that could help me, by pointing me in the right direction :)

Cheers

/Rex

Share this post


Link to post
Share on other sites



 

Look at StringLeft, StringTrimLeft and other string functions. That's all you need.

:

Local $sTest = "123456789"

; Replace the third character with X
$sTest = StringLeft($sTest, 2) & "X" & StringTrimLeft($sTest, 3)

MsgBox(0, "", $sTest)

Hi.

I'm not replacing it just placing it :)

I'm not sure that I explained my self correct.

My script should out put some thing like:

StringwriteLine("| " & $newdata & $place_at_80 & "|"

I know that the last right chr is a position 80 in the template, but not how to put i there depending on typed in data :(

Cheers

/Rex

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

First get the length of the string to be placed using StringLen(). Do some maths to work out where the string needs to be placed so the last character appears at position 80. Then use StringLeft() and StringTrimLeft() to place the string at the position you want. The code below might not be exactly what you want, but the required process will be similar.

;

Local $sTest = "123456789"
Local $sNew = "NEW"

; The last character 'W' should appear at position 8
Local $iStartLen = 8 - StringLen($sNew) ; Work out the position to insert the new string

; Insert the string so the last character 'W' appears at position 8
$sTest = StringLeft($sTest, $iStartLen) & $sNew & StringTrimLeft($sTest, $iStartLen)

MsgBox(0, "", $sTest)
Edited by czardas

Share this post


Link to post
Share on other sites

So i need to do some thing like:

$data = 123456

$Math1 = stringlen($data)

77 - $math1 (71) (first char is | 2. is ws 3. string start)

$AddWS = add 70 whitespaces & "|"

Is that correct?

Cheers

/Rex

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

So i need to do some thing like:

$data = 123456

$Math1 = stringlen($data)

77 - $math1 (71) (first char is | 2. is ws 3. string start)

$AddWS = add 70 whitespaces & "|"

Is that correct?

Cheers

/Rex

 

If you need to add spaces then do that. Also try the function JohnOne just mentioned. Make sure you think through all possible outcomes and check your results just in case there are any bugs. You should be able to figure it out yourself now. Ask again if you have any more problems. :)

Edited by czardas

Share this post


Link to post
Share on other sites

 

Or use a ready rolled.

_StringInsert

Darn

Why didn't I find that when i searched  :

Thx JohnOne, just what i was looking for :)

Cheers

/Rex

Share this post


Link to post
Share on other sites

If you need to add spaces then do that. Also try the function JohnOne just mentioned. Make sure you think through all possible outcomes and check your results just in case there are any bugs. You should be able to figure it out yourself now. Ask again if you have any more problems. :)

Ill do that

Thx for helping :)

Cheers

/Rex

1 person likes this

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  
Followers 0