forked from task032015/stringProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringProblem.java
More file actions
92 lines (67 loc) · 2.75 KB
/
Copy pathStringProblem.java
File metadata and controls
92 lines (67 loc) · 2.75 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.apple;
import org.testng.Assert;
import org.testng.annotations.Test;
public class StringProblem {
//Main function to run removeCharWithIteration() and removeCharWithStrMethod() methods
/*public static void main(String args[]){
System.out.println(removeCharWithIteration());
System.out.println(removeCharWithStrMethod());
}*/
//TestNG test cases
//For valid string and valid character present in the string (Iteration Method)
@Test
public void testRemoveCharWithStrMethod(){
String givenString="Mississippi";
char charToReplace='i';
String stringToReplace=String.valueOf(charToReplace);
String modifiedString=removeCharWithStrMethod(givenString,charToReplace);
Assert.assertFalse(modifiedString.contains(stringToReplace));
}
//For valid string and valid character not present in the string (Iteration Method)
@Test
public void testRemoveCharWithStrMethodNegative(){
String givenString="Mississippi";
char charToReplace='z';
String stringToReplace=String.valueOf(charToReplace);
String modifiedString=removeCharWithStrMethod(givenString,charToReplace);
Assert.assertFalse(modifiedString.contains(stringToReplace));
}
////For valid string and valid character present in the string (String Method)
@Test
public void testRemoveCharWithIteration(){
String givenString="Mississippi";
char charToReplace='i';
String stringToReplace=String.valueOf(charToReplace);
String temp=removeCharWithIteration(givenString, charToReplace);
Assert.assertFalse(temp.toString().contains(stringToReplace));
}
//For valid string and valid character not present in the string (String Method)
@Test
public void testRemoveCharWithIterationNegative(){
String givenString="Mississippi";
char charToReplace='z';
String stringToReplace=String.valueOf(charToReplace);
String temp=removeCharWithIteration(givenString, charToReplace);
Assert.assertFalse(temp.toString().contains(stringToReplace));
}
//Method to remove character from String iteratively (one by one)
public static String removeCharWithIteration(String givenString, char charToReplace){
//String givenString="Mississippi";
//char charToReplace='i';
StringBuilder modifiedString=new StringBuilder();
for(int i=0;i<givenString.length();i++){
char temp=givenString.charAt(i);
if(temp!=charToReplace){
modifiedString.append(temp);
}
}
return modifiedString.toString();
}
//Method to remove character from String using String method(in a line)
public static String removeCharWithStrMethod(String givenString, char charToReplace){
//String givenString="Mississippi";
//char charToReplace='i';
String modifiedString=givenString.replaceAll(Character.toString(charToReplace), "");
return modifiedString;
}
}