• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

trying .repeat for string

Explorer ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

For some reason my .repeat isnt working for a text string.

 

If i wanted five 0's

I wanted '0'.repeat(5);

not working. 

any help is appreciated.

 

TOPICS
Scripting

Views

458

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Mar 19, 2021 Mar 19, 2021

It would be best if you rewrote that repeat function as a separate method, so you could simply use this: 

sName = sName + repeatString('0', zeroAmount);. This way you don't have to use a nested for loop, like you have it right now.
 
And the repeatString function would look like this:
function repeatString(string, times) {
	var result = '';
    for (var i = 0; i < times; i++) {
        result += string;
    }

    return result;
}

Votes

Translate

Translate
Advocate ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

In what context are you using this method? C++, Expression, CEP extension, or ExtendScript?

If it's ExtendScript, then this method will not work, since ExtendScript uses very old JavaScript version (ECMA version 3.0) that does not have this method. You'll have to use the for loop to concatinate strings to achive what you are afterr.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

Thank you. Yes. Extendscript. This makes sense. I'll jump in and give it a shot. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

mName = "My_Diffuse_[00000-00030].png" //establish name

numSlice = mName.lastIndexOf("["); //get number index
numSlice2 = mName.lastIndexOf("-"); //get number index
zeroAmount = numSlice2-numSlice; // calculate amount of characters needed

sName = mName.slice(0, numSlice); //slice mName based on numSlice. I remove the "[00000-00030].png"

//this for loop i wat to add the proper amount of 0's at the end of the sliced file name
for (i = 1; i < zeroAmount; i++){
    sName.concat("0");
}
  
//show me result.
alert(sName);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

Above is my latest. I think im doing things right, its just not working.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

Your for loop is incorrect. Try this instead:

for (i = 1; i < zeroAmount; i++){
  sName += "0";
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

thank you. That did it.
Minor thing that i worked around is now this for loop breaks the for loop
it was embedded in.
Ill try to solve that but if i added multiple if statements it
didnt break the loop. weird

--
Regards,
Thomas Maloney

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

LATEST

It would be best if you rewrote that repeat function as a separate method, so you could simply use this: 

sName = sName + repeatString('0', zeroAmount);. This way you don't have to use a nested for loop, like you have it right now.
 
And the repeatString function would look like this:
function repeatString(string, times) {
	var result = '';
    for (var i = 0; i < times; i++) {
        result += string;
    }

    return result;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

A @Tomas Sinkunas says.

Here you find a polyfill:

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

You can copy and paste the code of the polyfill in your program and it will add the missing repeat method.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines