cancel
Showing results for 
Search instead for 
Did you mean: 

right Justify a string with a fixed length

Former Member
0 Kudos

Hello Java experts,

I would like to seek advise from you for the codes i've needed to create a UDF that can right Justify a certain string.

Example,

The field has values on the queue,

12

1

3

3

4

The field has a certain fixed length of 10.

So the output must be,

________12

_________1

_________3

_________3

_________4

The "___" symbol indicates white spaces.

The field will be mapped on a UDF, that will perform the desired right Justification.

Can somebody help me on this?

Thank you very much in advance.

Fred

Edited by: Fred Lampa on Apr 16, 2008 3:55 PM

Accepted Solutions (0)

Answers (3)

Answers (3)

prateek
Active Contributor
0 Kudos

U can try something like this

int a = 0;

while( a < 10){

no = " " + no;

a++

}

Regards,

Prateek

Former Member
0 Kudos

Hi Fred,

You can right justify the values without using UDFs.

First, append field with 10+ blank chars using concat, and then substring it with 10 chars.

Regards,

AV.

Former Member
0 Kudos

Hi AV,

I guess you want to take the last ten chars with the substring-function.

For this you will have to know how long your string is, don't you?

Regards

Patrick

Former Member
0 Kudos

Hi Patrik,

Following up to your comment...

> The field has values on the queue,

> 12

> 1

> 3

> 3

> 4

>

> The field has a certain fixed length of 10.

>

> So the output must be,

> ________12

> _________1

> _________3

> _________3

> _________4

From the question posted above, I assumed his requirement was to right justify integer values with 10 fixed width.

It will be great if you point the problem with solution I suggested.

Thanks,

AV.

Former Member
0 Kudos

Hi,

if I understand you right you want add ten space chars and then you want to take a substring with the last 10 chars.

But if you add 10 chars you never know how long your result is.

For example if your input is 1234 your result will have a length of 14 and

with an input like 2 your result have a lenght of 11.

But you will have to know the length of the string to use the substring function (if you want to take the last ten chars).

Regards

Patrick

Former Member
0 Kudos

Yes Patrick, thats what I suggested.

Now the field has fixed length of 10, how can we pass value if input has 11 chars. Agreed that substring(10) will be meaningless to pass in that case (which I suggested).

>The field has a certain fixed length of 10

If input value has 10+ chars and output field can accomodate only 10 chars, how will your solution work in that case.

I hope I am not annoying you too much 😛

Best Regards.

Former Member
0 Kudos

Hi,

> I hope I am not annoying you too much 😛

No, don't worry about it...

> The field has a certain fixed length of 10

This is about the target field, right?

So the source field has between 1 and 10 chars and should be filled with blanks to have the fixed length of 10 chars.

That's what my function is doing and it's not working with substring if you don't know the length of the input string.

If the input has more than 10 chars it makes no sense as you have to reduce it to 10 chars. If it's possible it would make sense to check this in the udf like I already mentioned above and what could look like:

if (a.length() > 10) {

a = " ";

return a;

}

or you take the last ten chars, or whatever...

Would be interesting to know how Fred solved it

Regards

Patrick

Former Member
0 Kudos

Hi,

this should work:

while (a.length() < 10) {

a = " " + a;

}

return a;

If your input could be have a greater length then 10 then you should check this otherwise your while-loop will be infinite.

Regards

Patrick