0

I'm looking for a performance solution in multi threading processes in JSE 1.7.0.52 for this problem:

I have a String for example: " 12697 26 "

I need to have a string as "00012697 26 "

This means that I can't use String.formatter method as:

String c = "   12697 26    ";<br />
String.format( "%1$15s", c);<br /><br />

Because the result will be the same of the input. The 15s fill the string on left with spaces but I need to fill by zeros....

Some performace solution exists in new version of Java ? Kind regards

  • 1
    Why do you need this? – user207421 Feb 17 '14 at 09:32
  • @EJP Because I generate by listagg in oracle 11g a lot of string as a "vector in other system" concatenation for an integration partitioned by an attribute and now I have this problem. – SkyBlackHawk Feb 17 '14 at 09:35
  • @Karna It isn't a duplicate please read carefully the question... – SkyBlackHawk Feb 17 '14 at 09:36
  • I don't like to use RPAD in oracle because the sql is very heavy as costs ... – SkyBlackHawk Feb 17 '14 at 09:37
  • 1
    Why are there exactly 3 zeros? – Henry Feb 17 '14 at 09:37
  • @Henry because I need to send this data to a file for host system and the track defined for this file have this field defined for 15 characters... – SkyBlackHawk Feb 17 '14 at 09:38
  • 1
    But your example has only 12 characters ... – Henry Feb 17 '14 at 09:39
  • *Why* does the 'track defined for this file have this field defined for 15 characters' with a space in the middle but leading zeros to the left? The impression here is that you don't understand the requirement yourself. NB the cost of an `RPAD` is trivial compared to the cost of the query and retrievals. – user207421 Feb 17 '14 at 09:39
  • @Henry In editing the string is write as 15 character but when I publish the question the front end of stackoverflow site is show as a cut string ... – SkyBlackHawk Feb 17 '14 at 09:42
  • People the problem is described.... I have a string with spaces in the middle and in front of first character different of space... I need to right pad respect the first not white space..... – SkyBlackHawk Feb 17 '14 at 09:43
  • Merely repeating yourself doesn't answer the question. – user207421 Feb 17 '14 at 09:46

2 Answers2

1
String input = "   12697 26    ";
StringBuilder s = new StringBuilder(input);
for (int i = 0  ; i<s.length() && s.charAt(i) == ' ' ; i++) {
    s.setCharAt(i, '0');
}

System.out.printf("'%s'", s.toString());

'00012697 26 '

tddmonkey
  • 19,324
  • 9
  • 53
  • 66
  • Wrong solution on tail string I don't need zeros on tail... – SkyBlackHawk Feb 17 '14 at 09:48
  • updated to reflect your requirements now – tddmonkey Feb 17 '14 at 09:55
  • Thanks @MrWiggles this is the same solution that I'm using. The problem is that I managing 1.2gb of file in output with 6.000.000 of records in concatenation.... and this solution is heavy ... – SkyBlackHawk Feb 17 '14 at 10:17
  • What do you mean by "heavy". This is only a memory copy into the StringBuilder and will iterate *only* for the leading spaces. Have you done any profiling to see if this won't work for you? – tddmonkey Feb 17 '14 at 10:19
  • I'm searching now to use awk I suppose that it's more performance before to send the result file... People, thanks a lot for your help ... – SkyBlackHawk Feb 17 '14 at 10:39
  • If you're doing work with the file in Java and then passing the output to awk to add the zeroes it most certainly will _not_ be more performant – tddmonkey Feb 17 '14 at 11:07
0

Code is..

String func(String s)
{
    int flag=0;
    String temp="":
    for(int i=0;i<s.length();i++)
    {
        if(flag==0&&s.charAt(i)==' ')
        temp+="0";
        else
        {
            flag=1;
            temp+=s.charAt(i);
         }
    }
    return temp;
  }
Devavrata
  • 1,655
  • 13
  • 26