forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path122_leftPad.java
34 lines (33 loc) · 1.05 KB
/
122_leftPad.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class StringUtils {
/**
* @param originalStr the string we want to append to with spaces
* @param size the target length of the string
* @return a string
*/
static public String leftPad(String originalStr, int size) {
// Write your code here
if(originalStr==null)
return null;
int length = originalStr.length();
for(int i=0; i<size-length; i++){
originalStr = " "+originalStr;
}
return originalStr;
}
/**
* @param originalStr the string we want to append to
* @param size the target length of the string
* @param padChar the character to pad to the left side of the string
* @return a string
*/
static public String leftPad(String originalStr, int size, char padChar) {
// Write your code here
if(originalStr==null)
return null;
int length = originalStr.length();
for(int i=0; i<size-length; i++){
originalStr = padChar+originalStr;
}
return originalStr;
}
}