You won't be able to modify the methods that are built into the String
class, so instead I think it's best to write static
utility methods to reproduce the behaviour of the C# methods.
The code might look something like this (though be sure to write your own unit tests before using these methods, or any other methods you write or introduce).
public static void main(String[] args) {
String original = "text";
int padToLength = 10;
System.out.println("Padded:\n'" + padRight(original, padToLength) + "'");
System.out.println("Padded:\n'" + padRight(original, padToLength, '@')
+ "'");
}
public static String padRight(String original, int padToLength) {
return padRight(original, padToLength, ' ');
}
public static String padRight(String original, int padToLength, char padWith) {
if (original.length() >= padToLength) {
return original;
}
StringBuilder sb = new StringBuilder(padToLength);
sb.append(original);
for (int i = original.length(); i < padToLength; ++i) {
sb.append(padWith);
}
return sb.toString();
}
This will output to the console:
Padded with spaces:
'text '
Padded with custom character:
'text@@@@@@'