Reputation: 115
In my program i want to take an input from a user, and separate the words with the letter "e" in them and concatenate them without any spaces or different lines. For instance, if my input was "Rob is a nice person" I want it to print "niceperson". This is what i have so far:
Scanner kybd = new Scanner(System.in);
String s = kybd.nextLine();
String[] arr = s.split(" ");
for ( String ss : arr) {
String []ary = {ss};
for(int i = 0; i < ss.length(); i++){
if(ary[i].equalsIgnoreCase("e")){
System.out.print(ary);
}
}
}
Thanks for any tips or help!
Upvotes: 2
Views: 94
Reputation: 135992
try this
s = s.replaceAll("(\\b[\\w&&[^e]]+\\b)|\\s", "");
Upvotes: 0
Reputation: 783
how about some method like string.Replace("e ", "e"); I assume you are using String static method, as I can see you are using Split
Upvotes: 0
Reputation: 35547
You can do it in this way
String str = "Rob is a nice person";
String[] arr = str.split(" "); // split by space
StringBuilder sb = new StringBuilder();
for (String i : arr) {
if (i.contains("e")) { // if word contains e
sb.append(i);// append that word
}
}
System.out.println(sb);
Out put:
niceperson
Upvotes: 1
Reputation: 3120
Use the contains
method, it works like this:
Scanner kybd = new Scanner(System.in);
StringBuilder result = new StringBuilder();
String s = kybd.nextLine();
String[] arr = s.split(" ");
for ( String ss : arr) {
if(ss.contains("e")) {
result.append(ss);
}
}
Upvotes: 3