forked from task032015/stringProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCharFromString.java
More file actions
63 lines (52 loc) · 1.54 KB
/
Copy pathRemoveCharFromString.java
File metadata and controls
63 lines (52 loc) · 1.54 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
Define a Java class to solve this problem:
Given a String and a Character, remove all instances of the Character in the String
Solve this two ways:
1. Iterate through the String, one character at a time
2. Find a method in the String class that can solve this in one line
Write methods for each solution.
Afterwards, write a TestNG or JUnit class for each solution which tests these methods.
Include both positive and negative cases for validations.
*/
/**
* @author Vlad
*
*/
public class RemoveCharFromString {
public static String str = "Apple interview";
public static char ch = 'p';
public String RemCharMethod1(String s, char c) {
if (s == null)
return null;
String res = "";
char[] chstr = s.toCharArray();
for (char celem : chstr) {
if (celem != c)
res += celem;
}
return res;
}
public String RemCharMethod2(String s, char c) {
if (s == null)
return null;
String res = "";
res = s.replace(String.valueOf(c), "");
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length < 2)
System.out.println("Using default params");
else {
str = args[0];
ch = args[1].charAt(0);
}
System.out.println("Original string = " + str);
RemoveCharFromString rcfs = new RemoveCharFromString();
System.out.println("Result for method 1 = " + rcfs.RemCharMethod1(str, ch));
System.out.println("Result for method 2 = " + rcfs.RemCharMethod2(str, ch));
}
}