Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,247 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,225 people online right now. Registration is fast and FREE... Join Now!




Print string several times

 
Reply to this topicStart new topic

Print string several times

dufyd
13 Oct, 2008 - 05:49 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 5

CODE
    public String RepeatAtFrontAndEnd(String str, int nChars, int nRepeats){
        int chars = nChars;
        int repeats = nRepeats;
        int i;
        String sentence = str;
        if(chars == 0 || repeats == 0){
            sentence = str;
        }
        else if(str.length()<chars){
            sentence = str;
        }
        else
        
            for(i=0; i < repeats; i++){
                String toRepeat = (str.substring(0, chars));
                sentence = (toRepeat + str + toRepeat);                
        }
        return sentence;
        
        

    }


Ok. I think I have everything I need here exept that when I need to print a part of a string (toRepeat) in the front and end of the original string. I have that, what I need now is to print it (repeats) times in the front and in the end.

Can anyone help? thank you.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Print String Several Times
13 Oct, 2008 - 06:58 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you want to repeat part of the string and put it onto the start and end of the original string you can use this version of your function...

java

public static String RepeatAtFrontAndEnd(String str, int nChars, int nRepeats){
int chars = nChars;
int repeats = nRepeats;
int i;
String sentence = str;
if(chars == 0 || repeats == 0){
sentence = str;
}
else if(str.length()<chars){
sentence = str;
}
else {
String toRepeat = "";

// Create the repeat string itself "repeat" number of times
for(i=0; i < repeats; i++){
toRepeat += (str.substring(0, chars));
}

// Then tack in onto the start and end of the str
sentence = (toRepeat + str + toRepeat);
}


return sentence;
}


Just read through the in-code comments to see what I did. Should make some sense. Enjoy!

"At DIC we be codcodcode ninjascodcod..... nuff said!" decap.gif

This post has been edited by Martyr2: 13 Oct, 2008 - 06:59 PM
User is offlineProfile CardPM
+Quote Post

dufyd
RE: Print String Several Times
13 Oct, 2008 - 07:19 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 5

ok now it works! thanks, I am still unsure...it seems that what makes it work is the += instead of = in:

toRepeat += (str.substring(0, chars));
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:53AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month