# Equivalent string() vb in xojo

Hello,
in visual basic 6 string function is working like this:
String(5,"*") , returns ***** .
How can i do this under xojo, does any equivalent ?

make a for loop. Maybe like this:

[code]Function nstring(c as integer, s as string) As string
if c = 0 then
Return “”
elseif c = 1 then
Return s
else
dim values() as string

``````redim values(c-1)
for i as integer = 1 to c
values(i) = s
next

Return Join(values, "")
``````

end if

End Function[/code]

Thank you Christian.
it runs properly like this :

if c = 0 then
Return “”
elseif c = 1 then
Return s
else
dim values() as string

``````redim values(c)    ' to avoid OutOfboundsException.

for i as integer =1 to c
values(i) = s
next

Return Join(values,"")
``````

end if

It would be better to correct Christian’s code as

``for i as integer = 0 to c-1``

My M_String module has a Repeat function and much more. See

[quote=267894:@Tim Hare]It would be better to correct Christian’s code as

``` for i as integer = 0 to c-1 ```[/quote]
Hello Tim,
with this way nstring returns c-1 characters.

[code]
if c = 0 then
Return “”
elseif c = 1 then
Return s
else
dim values() as string

``````redim values(c-1)  // <<<<<<<<<< dont fix this one !!!!!!!!!

for i as integer =1 to c
values(i-1) = s // <<<<<<<<<<< change this one
next

Return Join(values,"")
``````

end if[/code]

Thank you Norman, very nice !

Sorry for my Off by One Error.

``values(i-1) = s ``

is right

or maybe

```dim u as integer = c -1 for i as integer = 0 to u values(i) = s next```

[quote=267974:@Christian Schmitz]Sorry for my Off by One Error.

``values(i-1) = s ``

is right

or maybe

```dim u as integer = c -1 for i as integer = 0 to u values(i) = s next```[/quote]

[quote=267894:@Tim Hare]It would be better to correct Christian’s code as

``` for i as integer = 0 to c-1 ```[/quote]
Christian, this way does not run properly.

well, you have to decide.
Several changes are suggested here, but you need to check what works.
So either you use 0 to c-1 or you use i-1. But both will not work as you get a -1 array access.

[quote=267968:@Djamel AIT AMRANE]Hello Tim,
with this way nstring returns c-1 characters.[/quote]
No. 0 to c-1 is c characters. If you’re getting a different result, then you made some other change to Christian’s code.

For a small number of repeats with a small string, it won’t much matter, but this is the fastest algorithm I’ve found. (Adapted from the one found in StringUtils.)

``````Function Repeat_MTC(Extends src As String, repetitions As Integer) As String
// Repeats the given string repetitions times.

// Common cases
if repetitions < 1 then return ""
if repetitions = 1 then return src
if repetitions = 2 then return src + src
if repetitions = 3 then return src + src + src

dim curLenB as integer = src.LenB
dim targetLenB as integer = curLenB * repetitions
dim halfLenB as integer = ( targetLenB + 1 ) \\ 2

while curLenB < halfLenB
src = src + src
curLenB = curLenB + curLenB
wend

dim diffB as integer = targetLenB - curLenB
if diffB <> 0 then src = src + LeftB( src, diffB )

return src

End Function``````

In a case where I repeated a sentence 1,000 times, this algorithm was around 4-5 times faster than using an array.