Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.List;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class Tehtava1 {
- public static void main(String[] args) {
- trimAndSaveSmileys("Moi! :) Mitä (ihmettä?) sinulle kuuluu tänään (; =D D= :/ :{");
- }
- public static ArrayList<String> trimAndSaveSmileys(String str){
- //split every word
- String [] strings = str.split(" ");
- //Add all combinations that are considered as smileys
- ArrayList<String> allowedSmileys = new ArrayList<>(Arrays.asList(
- ":)",
- ";)",
- "(:",
- "(;",
- "=D",
- "D=",
- ":/",
- ":{")
- );
- ArrayList<String> fixedStrList = new ArrayList<String>();
- boolean smileyFound;
- for(int i=0;i<strings.length;i++){
- //check if word is one of allowed smileys
- smileyFound = false;
- for (String smiley : allowedSmileys) {
- if(strings[i].equals(smiley)){
- smileyFound = true;
- }
- }
- if(smileyFound == true){
- //smiley no need to trim
- fixedStrList.add(strings[i]);
- } else {
- //not smiley so replace everything except numbers and letters
- strings[i] = strings[i].replaceAll("[^a-zA-Z0-9äÄöÖ]", "");
- fixedStrList.add(strings[i]);
- }
- }
- System.out.println(fixedStrList);
- return fixedStrList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement