You need to sign in to do that
Don't have an account?
Uves Ravat
Padding
Hi
I have been trying to use the following method doesnt work. Here is the code
public static String padRight(String s, integer n) { return String.format('%1$-' + n + 's', s); }
I am trying to pad my strings.
for example if i have two words from and yesterday, i want pad from so from would be fromxxxxx. i want the padding on every word in the list to make all the words in the string have the same characters, Like in this example from and yesterday would have the same number of characters
Thanks
Uves
http://boards.developerforce.com/t5/Apex-Code-Development/insert-predefined-amount-of-balnk-spaces-between-word-or-before/m-p/360835#M63995
See my discussion here; I provide two example functions (leftpad and rightpad) that illustrate a simple padding technique. In short, you create a string of spaces, then take substrings of that string to create padding without looping repeatedly. Depending on how many spaces you need and how many rows to process, this might be tons more effective than a simple loop.
All Answers
You should name your parameters. Is 'n' the total length after padding, or the amount of padding to add?
Anyways, the naive solution would be something like:
Not very elegant, but it should work for now, and when you get more time you can make it better later.
Sorry Bryan, I was very vague with n. N should the length of the longest word in the list. it should only add padding to words that has less characters than the longest word
Thanks
Hi,
What i need it that i will an object with three values. For example as followers:
Object x ' Product Name' Employee Name' Date
Object 1 'GenWatt Diesel Adam Smith 1000kW 2012-04-06 00:00:00'
Object 2 'Annual Service Contract - Small Business Editions Adam Smith 2012-04-06 00:00:00'
Object 3 ' Annual Service Contract - Small Business Editions Cassie Booker 2012-07-26 00:00:00'
I want to put those in order, firstly by the product name then by employee and the finally by date?
What is the best way to do it? i though i will padd them all (which i am struggling to do) and then sort them?
So after padding it would look like this and then i would sort it
Object 1 'GenWatt Diesel 1000kW Adam Smith 2012-04-06 00:00:00'
Object 2 'Annual Service Contract - Small Business Editions Adam Smith 2012-04-06 00:00:00'
Object 3 'Annual Service Contract - Small Business Editions Cassie Booker 2012-07-26 00:00:00'
Thanks
http://boards.developerforce.com/t5/Apex-Code-Development/insert-predefined-amount-of-balnk-spaces-between-word-or-before/m-p/360835#M63995
See my discussion here; I provide two example functions (leftpad and rightpad) that illustrate a simple padding technique. In short, you create a string of spaces, then take substrings of that string to create padding without looping repeatedly. Depending on how many spaces you need and how many rows to process, this might be tons more effective than a simple loop.
worked as a peach.
Thanks :)
If you only need it sorted on the UI, you can edit the page layout to sort by columns.
If you need to do it in code the best way to sort your own object in a list is to have your object implement comparable.
if the objects are actually database records, wrap them in a class that implements comparable.
Unfortunatly there is no sort method that takes a comparator.
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_lists.htm#heading_3_1
I wouldn't recommend printing all the fields to string, padding, appending them and then sorting them with the resulting string.
String s = 'AAAA';
Integer i =10;
String r = s+i.format().leftPad(8).replaceAll(' ','0');
System.Debug('-'+r+'-');