Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello fellow programmers!

I've made this whitespaceIndent method, for creating a indentation string out of whitespaces.
My goal was to create a method, that replaces tabs.
So fx, if I used tabs, this would be my output:
Java
Directory          Size
C:/Games           126 MB
C:/Program Files            259 MB

But if I used the whitespaceIndent method, this would be my output:
Java
Directory          Size
C:/Games           126 MB
C:/Program Files   259 MB

It's doing this by taking the total size of the indentation, and a x amount of removal indentation.
So in this case, the total size of the indentation would be between index 0 and the beginning index at Size. It when subtracts the length of C:/Program Files from the total indentation.

But now I'm wondering, is there a easier way of doing this?
If you know any, please let me know.

Thanks in advanced - Neven.

The code I'm referring to:
Java
// Returns a whitespace string, for perfect text indentation.
// It creates the string using the total length (total) and the removal length (removal) 
public static String whitespaceIndent(int total, int removal) {
	
	// Create the whitespace string to return
	String whitespace = "";
	
	// Adds a whitespace (total - removal) times
	for (int i = 0; i < total - removal; i ++)
		whitespace += " ";

	// Return the whitespace
	return whitespace;
}
Posted
Comments
Richard MacCutchan 30-Aug-15 4:15am    
You could just use the count value (total - removal) to generate a string of spaces. Or use it directly as the position to write the next oputput field.
Oliver S. Neven 30-Aug-15 7:47am    
I'm just making a string out of the (total - removal) many spaces.
But I want to know if you can do this without a loop.
Richard MacCutchan 30-Aug-15 10:25am    
Yes, see the documentation for String.format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900