-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutX.java
More file actions
32 lines (25 loc) · 884 Bytes
/
withoutX.java
File metadata and controls
32 lines (25 loc) · 884 Bytes
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
public String withoutX(String str) {
String result = "";
if (str.length() < 1 || str.equals("x")) {
return result;
}
if (str.substring(0,1).equals("x") && str.substring(str.length()-1).equals("x")) {
result = str.substring(1,str.length()-1);
}
if (str.substring(0,1).equals("x") && !str.substring(str.length()-1).equals("x")) {
result = str.substring(1);
}
if (!str.substring(0,1).equals("x") && str.substring(str.length()-1).equals("x")) {
result = str.substring(0,str.length()-1);
}
if (!str.substring(0,1).equals("x") && !str.substring(str.length()-1).equals("x")) {
result = str;
}
return result;
}
/*
Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.
withoutX("xHix") → "Hi"
withoutX("xHi") → "Hi"
withoutX("Hxix") → "Hxi"
*/