Creating a String with repeated chars

Is there a way to create a String with N repeated chars without using a loop?
I want to create something like "aaaaaaaaaa" without having to do:
    StringBuffer s = new StringBuffer();
    for(int i = 0; i< 10; i++){
      s.append("a");
    }

Ahh, Groovy.
#! /usr/bin/groovy
s = 'a' * 10
assert s == 'aaaaaaaaaa'http://groovy.codehaus.org/
:o)

Similar Messages

  • How to create a String with a specific size?

    how to create a String with a specific size?
    For example I want to create different Strings with the size of 100 , 1000 or 63k byte?

    String are immutable so just initialize it with the number of characters you want.
    You might want to look at java.lang.StringBuffer and see if that's what you want.

  • How to improve the speed of creating multiple strings from a char[]?

    Hi,
    I have a char[] and I want to create multiple Strings from the contents of this char[] at various offsets and of various lengths, without having to reallocate memory (my char[] is several tens of megabytes large). And the following function (from java/lang/String.java) would be perfect apart from the fact that it was designed so that only package-private classes may benefit from the speed improvements it offers:
        // Package private constructor which shares value array for speed.
        String(int offset, int count, char value[]) {
         this.value = value;
         this.offset = offset;
         this.count = count;
        }My first thought was to override the String class. But java.lang.String is final, so no good there. Plus it was a really bad idea to start with.
    My second thought was to make a java.lang.FastString which would then be package private, and could access the string's constructor, create a new string and then return it (thought I was real clever here) but no, apparently you cannot create a class within the package java.lang. Some sort of security issue.
    I am just wondering first if there is an easy way of forcing the compiler to obey me, or forcing it to allow me to access a package private constructer from outside the package. Either that, or some sort of security overrider, somehow.

    My laptop can create and garbage collect 10,000,000 Strings per second from char[] "hello world". That creates about 200 MB of strings per second (char = 2B). Test program below.
    A char[] "tens of megabytes large" shouldn't take too large a fraction of a second to convert to a bunch of Strings. Except, say, if the computer is memory-starved and swapping to disk. What kind of times do you get? Is it at all possible that there is something else slowing things down?
    using (literally) millions of charAt()'s would be
    suicide (code-wise) for me and my program."java -server" gives me 600,000,000 charAt()'s per second (actually more, but I put in some addition to prevent Hotspot from optimizing everything away). Test program below. A million calls would be 1.7 milliseconds. Using char[n] instead of charAt(n) is faster by a factor of less than 2. Are you sure millions of charAt()'s is a huge problem?
    public class t1
        public static void main(String args[])
         char hello[] = "hello world".toCharArray();
         for (int n = 0; n < 10; n++) {
             long start = System.currentTimeMillis();
             for (int m = 0; m < 1000 * 1000; m++) {
              String s1 = new String(hello);
              String s2 = new String(hello);
              String s3 = new String(hello);
              String s4 = new String(hello);
              String s5 = new String(hello);
             long end = System.currentTimeMillis();
             System.out.println("time " + (end - start) + " ms");
    public class t2
        static int global;
        public static void main(String args[])
         String hello = "hello world";
         for (int n = 0; n < 10; n++) {
             long start = System.currentTimeMillis();
             for (int m = 0; m < 10 * 1000 * 1000; m++) {
              global +=
                  hello.charAt(0) + hello.charAt(1) + hello.charAt(2) +
                  hello.charAt(3) + hello.charAt(4) + hello.charAt(5) +
                  hello.charAt(6) + hello.charAt(7) + hello.charAt(8) +
                  hello.charAt(9);
              global +=
                  hello.charAt(0) + hello.charAt(1) + hello.charAt(2) +
                  hello.charAt(3) + hello.charAt(4) + hello.charAt(5) +
                  hello.charAt(6) + hello.charAt(7) + hello.charAt(8) +
                  hello.charAt(9);
             long end = System.currentTimeMillis();
             System.out.println("time " + (end - start) + " ms");
    }

  • Creating a string with hiearchical data

    This is being done in Crystal 8 will all updates applied.
    I have a database with records that are linked to a series of "folders" that are hierarchical in nature as shown below:
    Folder 1:
         Folder 1.1
              Folder 1.1.1
    Folder 2
        Folder 2.1
    All of the folders can have data records attached to them.
    The database represents the folder structure in a table with the following attributes:
    Folder ID   (integer)
    Folder Name (String)  (such as "Folder 1"
    Folder Parent ID (integer)
    (other non-related fields)
    If the folder is at the root level, then the value in Folder Parent ID is 0, otherwise, it is the Folder ID of the parent folder.
    I need to be able to create a string field that displays the path for any given folder that I can insert into a group header or footer, or into a report header if the report deals with records in a single folder.  Desired format for the  string is: 
    "Folder 1> Folder 1.1> Folder 1.1.1"
    I need to be able to display at least 6 levels of folder indentation.
    I have tried several ways to create a formula that will reliably produce the desired string, and can get close, but cannot completely address the problem.  My most successful approach was to put multiple instance of the table into the report and link the Folder Parent ID of one level to the Folder ID of the next level down and then use the data to build the string with a formula like this:
    Local StringVar Result := {portfolio_master.portfolio_name};
    local stringvar ExitLoop := "1" ;
    if  ExitLoop = "1" then
        Result := {portfolio_master_1.portfolio_name} + "> " + Result
    else
        ExitLoop := "0";
    if {portfolio_master_1.parent_id} NE 0 AND ExitLoop = "1" then  // NE replaces Crystal not equal symbol which will not show
        Result := {portfolio_master_2.portfolio_name} + "> " + Result
    else
        ExitLoop := "0";
    if {portfolio_master_2.parent_id} NE 0 and ExitLoop = "1" then
        Result := {portfolio_master_3.portfolio_name} + "> " + Result
    else
        ExitLoop := "0";
    if {portfolio_master_3.parent_id} NE 0 and ExitLoop = "1" then
        Result := {portfolio_master_4.portfolio_name} + "> " + Result
    else
        ExitLoop := "0";
    if {portfolio_master_4.parent_id} NE 0 and ExitLoop = "1" then
        Result := {portfolio_master_5.portfolio_name} + "> " + Result
    else
        ExitLoop := "0";
    Result;
    This formula works so long as the folder in question is exactly 5 levels deep.  Changing the nature of the links(inner join, left outer join etc.) between the instances of the table in the Database Expert changes the data that is visible, but I can't find a combination that will show ALL of the needed information for all of the folders. 
    I understand that this is a brute force approach, and that there must be a different way to skin the cat.  My preference would be to "walk the tree" with a loop structure, but I don't see any way to do the needed data look up within the loop.
    Any suggestions would be greatly appreciated!
    Thanks,
    L. Jasmann
    Edited by: ljasmann1 on Jun 23, 2010 9:09 PM
    Edited by: ljasmann1 on Jun 23, 2010 9:33 PM

    I have been working to adapt the code provided above to my application.  Although I have worked with Crystal for quite a while, this is the first time that I have attempted to use SQL in Crystal... and it is just not working.. probably because I am no doing something very basic.
    Here is  the code as currently set up in the SQL Formula Editor.  Only major difference from the code provided above is that the ID fields are integers instead of strings, and the field that I want to print out is a string field separate from the ID fields....  I have substituted the field names from the table I am referencing for the ones in the code above.  The table that I am using has been selected using the Database Expert.
    DECLARE @Value int, @pf int, @FolderPath VarChar(MAX)
    SET @Value = "portfolio_master"."portfolio_id"
    SELECT @pf = "portfolio_master"."parent_id" FROM portfolio_master WHERE "portfolio_master"."portfolio_id" = @Value
    SET @FolderPath = "portfolio_master"."portfolio_name"
    WHILE @pf <> 0
    BEGIN
    SET @Value = @pf
    SELECT @pf = "portfolio_master"."parent_id" FROM portfolio_master WHERE "portfolio_master"."portfolio_id" = @Value
    SET @FolderPath = "portfolio_master"."portfolio_name" + ' > ' + @FolderPath
    END
    SELECT @FolderPath AS FolderPath
    When I test the SQL statement it errors out on the very first statement (the DECLARE statement).  If I substitute a different statement in front of the DECLARE statement, it errors on that statement.
    Here is the error that I get back. The database is using MS SQL Server.
    Error in compiling SQL Expression:
    Failed to retrieve data from the database.
    Details:  ADO Error Code:  0x80040e14
    Source:  Microsoft OLE db Provider for SQL Server
    Description:  Incorrect syntax near the keyword u2018DECLAREu2019,
    SQL State:  42000
    Native Error:  156 [Database Vendor Code:  156].
    Any pointers would be greatly appreciated...
    Thanks,
    Larry Jasmann

  • How to create a String with comma ?

    Hi All,
    I have a table with 10 records(employee name) and i have to make a string
    with comma delimiter and at the end with "."
    Can anybody tell me how to write a Java program for this ?
    Thanks in advance.

    // I believe the following example gives you the answer.
    class stringEG {
         public static void main(String args[]) {
         String emprs[] ={"1","2","3","4","5","6","7","8","9","10"};
         String vempname = "";
         for(int i=0; i<emprs.length; i++) {
         if(i == (emprs.length-1))
              vempname = vempname + emprs[i] + ".";
         else
              vempname = vempname + emprs[i] + ", ";
         System.out.println("vempname : "+vempname);
    Here dont assume that I asked you to get all the results and putting
    it into the string arrays. But its a simple example to suit your requirement.
    The half-way coding below answers your question, I hope.
    vempname = "";
    while (emprs.next()) {
    if(emprs.isLast())
    vempname = vempname + emprs.getString("empname") + ".";
    else
    vempname = vempname + emprs.getString("empname") + ", ";
    // nats.

  • How to create a field with  1200 chars length

    Hi,
    Will anybody let me know how to create a field with length 1200 in a table.
    Regards,
    Madhavi

    Hi Madhvi,
    Other thing what you can do is
    1. First create a Table type.
         Goto SE11-> Dataelements ->Table type
         There on the next screen select Predefine radio button & there provide data type & size.
    2. Create a structure having a field typa of Table type created before. say that field is quant.
    Now in your program refer that structure to create an internal table & work area.
    Now if u append ur text to the field quant.
    try creating line type of size more that 255 if it works then fine other wise reduce it.
    Note:: This will be a deep structure.
    so first append value to the field quant,  the at the end of first record append valu to ur internal table.
    thanks
    Satyam

  • How to create a string with numbers, separated by ",".

    Hello!
    I have a start number and a end number. For example 101 and 110.
    Now I want to create a string containing all numbers between 101 and 110 separated by comma(",").
    (101,102,103 .......,110)
    Any smart suggestions?

    Hello EKrille,
    I changed the vi from tst to give the same results on both options...
    Best regards,
    GerdW
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome
    Attachments:
    string.vi ‏26 KB

  • Creating hair/string with trapcode particular

    hi
    currently trying to put together a graphic which includes string gently blowing in the wind from text
    i am using trapcode particular to put this together, but am open to suggestion in terms of other software.
    the reasoning for using particular as i like the use of gravity and turbulence which could create an effect of string blowing in the wind..a difficulty i am having is creating a line of particles to create the string, i have tried increasing the particles per second...put the dots aren't joining up!
    man i hope this makes sense to someone!
    AE 7, trapcode particular, 1.5 MAC
    oh...have a merry christmas!
    cheers
    Nate

    I've posted a preset at the page below:
    http://homepage.mac.com/aaron.s.cobb/FileSharing1.html
    It's called "Particular Streamer". I've put an expression on the Particles Per Second to make it easy to set it to multiples of the frame rate (this keeps the particles from jittering). Just put a whole number in there, and the expression will make particular spit out that many particles per frame.

  • How to repeatedly replace different strings with most chars are Not unique?

    NOTE: I'm trying to avoid to do a substring of a substring of a ... replacement.
    Hello there,
    I'm looking for solution on a best approach to do a string replacment in something like example below.
    The idea would be to:
    1. Open a file and Find a "Title" (i.e. TC162-01.2GFC (after <td colspan=6>)). The Title here will be a dynamic and can be changed from one run to another.
    2. If it match to expected value then do a replacement of data between <select... and </select> for another values.
    3. Keep going for each block of <tr> ... </tr>
    =======================================
    <tr>
    <td colspan=1>1</td>
    <td colspan=1>TC1.1</td>
    <td colspan=6>TC162-01.2GFC</td>
    <td colspan=2>Unknown</td>
    <td colspan=1><select name="res_1_0_20789_21522" size=1>
    <option>PASS</option><option>FAIL</option><option selected>No Status</option></select> </td>
    <td colspan=1><input type=text maxlength='30' size='10' name='comment_1' value=''></td></tr>
    <tr>
    <td colspan=1>2</td>
    <td colspan=1>TC1.2</td>
    <td colspan=6>TC162-01.3ABC</td>
    <td colspan=2>Unknown</td>
    <td colspan=1><select name="res_2_0_20790_21523" size=1>
    <option selected>PASS</option><option>FAIL</option><option>No Status</option></select> </td>
    <td colspan=1><input type=text maxlength='30' size='10' name='comment_2' value=''></td></tr>
    =======================================

    Here's a simple and conceptual example. You could use your real implementation of the getReplace() method.
    /** file: test-vlad.txt *************************************
    <tr>
    <td colspan=1>1</td>
    <td colspan=1>TC1.1</td>
    <td colspan=6>TC162-01.2GFC</td>
    <td colspan=2>Unknown</td>
    <td colspan=1><select name="res_1_0_20789_21522" size=1>
    <option>PASS</option><option>FAIL</option><option selected>No Status</option></select> </td>
    <td colspan=1><input type=text maxlength='30' size='10' name='comment_1' value=''></td></tr>
    <tr>
    <td colspan=1>2</td>
    <td colspan=1>TC1.2</td>
    <td colspan=6>TC162-01.3ABC</td>
    <td colspan=2>Unknown</td>
    <td colspan=1><select name="res_2_0_20790_21523" size=1>
    <option selected>PASS</option><option>FAIL</option><option>No Status</option></select> </td>
    <td colspan=1><input type=text maxlength='30' size='10' name='comment_2' value=''></td></tr>
    import java.util.regex.*;
    import java.io.*;
    public class Vlad{
      public static void main(String[] args) throws Exception{
        BufferedReader br;
        StringBuffer sb;
        String line, fcontent;
        sb = new StringBuffer();
        br = new BufferedReader(new FileReader("test-vlad.txt"));
        while ((line = br.readLine()) != null){
          sb.append(line + "\n");
        fcontent = sb.toString();
        String regex
         = "<td colspan=6>([^<]+)(</td>.+?<select[^>]+>)(.+?)</select>";
        Pattern pat = Pattern.compile(regex, Pattern.DOTALL);
        Matcher mat = pat.matcher(fcontent);
        StringBuffer rsb = new StringBuffer();
        while (mat.find()){
          String title = mat.group(1);
          String value = mat.group(3);
          String newValue = getReplace(title, value);
          mat.appendReplacement
            (rsb, "<td colspan=6>" + title + "$2" + newValue + "</select>");
        mat.appendTail(rsb);
        System.out.println(rsb.toString());
      } // end main()
      // a dummy implementation
      static String getReplace(String tdTitle, String selectValue){
        return "###ANOTHER VALUES###";
    }Edited by: hiwa on Dec 11, 2007 11:14 AM

  • Easiest way to create a string with a unicode supplimentary character

    In my code I had previously been declaring some strings such as:
    String s = "\u1234";Now I have gotten to a point where I want to use a supplimentary unicode character like this:
    String s = "\u12345";This, however, does not work since Java only allows for characters in the range: 0000 - FFFF.
    I've been reading over this whole codepoint thing in the Character docs but I'm still confused. What's the easiest way to make a string that contains the unicode character U+12345 (just an example number)?

    This article here:
    http://java.sun.com/developer/technicalArticles/Intl/Supplementary/
    says <quote>For text input, the Java 2 SDK provides a code point input method which accepts strings of the form "\Uxxxxxx", where the uppercase "U" indicates that the escape sequence contains six hexadecimal digits, thus allowing for supplementary characters.</quote>
    I believe it is referring to Java 5, although I don't see where it says that. However if you are dealing with supplementary characters it might be a good idea for you to move to Java 5 because of its improved support for them.
    PC&#178;

  • How to create a string with a slash in it.

    inputFile = String.valueOf("\saves\" + jcomp9.getText() + "\file.txt");I am trying to use a string as a filepath. I get error messages because of the slashes. What should I do?
    Thanks
    -Jason

    There is no need to use String.valueOf.
    Forward slashes are actually recognized as path separators:
    inputFile = "/saves/" + jcomp9.getText() + "/file.txt";If that is not acceptable, remember that backslash must be escaped:
    inputFile = "\\saves\\" + jcomp9.getText() + "\\file.txt";jcomp9? Please tell use that you have better variable names than that!

  • Creating email attachment with 255 char per record

    Hi,
    We want to generate an email with excel attachment through ABAP program. To do the same, we are trying to use FM 'SO_DOCUMENT_SEND_API1'.
    It looks like it only allows 255 characters per record however we want include upto 700 chracters per record. Does anyone know a way out to overcome this problem?
    Note - We do not want to wrap the text to next line.
    Thank you

    Hi:
    I would advice to make the copy of this function module SO_DOCUMENT_SEND_API1 and change the  packing_list-doc_size = '700'..
    Hopefully it would be helpful to solve the problem.
    Regards
    Shashi

  • Error with invalid chars found in characteristics

    Hi,
       I am loading information for this field EKPO-TXZ01 (PO text) into ODS and then into infocube. I have no issue loading this field content into ODS. However from ODS to infocube, I encountered error that says invalid chars found. (This field was defined as chars 40 in the infoobject). At BW customizing, the chars like #, " [] are being maintained. Please advise how to overcome this.

    Hi,
    From my experience I can say that:
    1. Not always assigning permitted characters helps. Especially if chars that you want to pass to the ODS are special chars. And there is too little space in the string with allowable chars.
    2. Setting a characteristics as a lower case char helps for its master data. An ODS will not allow to accept data with lower case letters anyway.
    3. There are several topics related to your question in this forum with solutions, even with ABAP codes.
    Look, for example at this one:
    https://www.sdn.sap.com/sdn/collaboration.sdn?contenttype=url&content=https%3A//forums.sdn.sap.com/profile.jspa%3FuserID%3D136828
    Best regards,
    Eugene

  • Can we create a structure with string type not char??

    Hi All
    Can we create a structure with type STRING not as a CHAR type??
    Please its urgent.
    Regards
    Deepanker

    HI,
    Yes, you can string instead of CHAR.
    Please follow the steps below :
    SE11 --> Table name --> Create Button --> And input the short description and necessary Delivery and Maintenance fields --> Select Fields tab --> Input some name on field --> And Click on Predefined button --> Press F4 on Data type column --> a Pop window will be displayed --> Select String from that.
    Thanks,
    Sriram Ponna.

  • Creating new String using only specific set of chars from another String

    I've performance troubles doing this task:
    I have a very long String A (up to 1 million character length)
    This String contains different characters.
    I need to create a String B containing only specific characthers (with a defined character code)
    An example:
    String A: [80]='P' [65]='A' [71]='G' [69]='E'For example I want to extract only character 65 and 71
    String B: [65]='A' [71]='G'I already did the code to do this, (it's quite simple) but I think I have some very big performance issues, and I suppose I can do this task much more quickly!!
    String out = "";
              for (counter = 0; counter < text.length(); counter++) {
                   char currentchar = text.charAt(counter);
                   int currentvalue = (new Integer(currentchar)).intValue();
                   switch (currentvalue) {
                   case 65:
                        char[] newstring = new char[1];
                        newstring[0] = currentchar;
                        out += new String(newstring);
                        break;
                   case 71: //
                        break;
                   // other cases
                   default:
                        // nothing to do...
              }Can you suggest me something to improve the performances?
    Thank you!

    If you've got a number of characters in your acceptance set I'd use indexOf, and I'd probably use StringCharacterIterator. Mind you, with a million characters I'd probably not load them all at once anyway, but process them as streams.
    StringBuffer out = new StringBuffer(s.length() / 5); // allocate plenty
    StringCharacterIterator it = new StringCharacterIterator(s);
    for(char c = it.first(); c != CharacterIterator.DONE; c = it.next())
      if("AG".indexOf(c) >= 0)
          out.append(c);
    return c.toString();

Maybe you are looking for

  • Hard drive networked through Time Capsule for Windows and Mac

    My previous setup was using an old Dell laptop with Windows XP to network share both my Samsung laser printer and my Seagate GoFlex 2 tb drive formated to NTFS, and all Windows 7, Windows 8, and OS X 10.6+ machines in the house were successfully able

  • Trouble with songs

    I have a lot of songs that have an exclamation point !- this, and when I click on the song to play it, it says cannot findit or whaere it came from, how do I grt rid of these and also is there an mp3 to wav and wav to mp3 and others is their a conver

  • FRM-99999 Network Error(Very Urgent)

    I have installed Oracle 8.0 & Forms 5.0 on NT 4.0 & also WAS 3.0 on the same Machine. But the problem, I am facing is that the forms show up, but we get FRM-99999 Network Error at any arbitary point of time . Please help Very urgent. Regards Sachin.

  • How do I update out of date package which is not mine?

    I want to update this package: http://aur.archlinux.org/packages/pdfedit Do I need to keep the previous contributer? Do I need to replace him by me? Do I not need to touch this at all since I do only a little changes? And what if it become big change

  • Cmd-W closes window in Safari... or does it?!

    Am I imagining it, or did Cmd-W use to close the active tab only? Now when I want to close a tab,  I hit Cmd-W and suddenly my entire Safari window closes. I notice in the File Menu that Cmd-W is set to close window and there is now no keyboard short