-
Notifications
You must be signed in to change notification settings - Fork 118
/
RemoveCharFromString.java
30 lines (28 loc) · 1.04 KB
/
RemoveCharFromString.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
package com.acn.demo.action;
import java.lang.String;
public class RemoveCharFromString {
static String input = "";
public static void main(String[] args) {
input = "axxbraxcadaxxbxrax";
char token = 'x';
removeChar(token);
removeCharOneLine(token);
}
private static void removeChar(char token) {
// TODO Auto-generated method stub
System.out.println(input);
for (int i=0;i<input.length();i++) {
if (input.charAt(i) == token) {
input = input.replace(input.charAt(i), ' ');
System.out.println("MATCHES FOUND AND REMOVED:");
}
}
input = input.replaceAll(" ", ""); // replace extara spaces for deleted chars
System.out.println(input+" - x removed with iteration");
}
private static void removeCharOneLine(char token) {
String replaceString;
replaceString = input.replace("x","");
System.out.println(replaceString+" - x removed with Java String replace() method");
}
}