Skip to content

Instantly share code, notes, and snippets.

@apietras
Last active October 15, 2016 14:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save apietras/ce299f08c4d43e34f566e8d9741bc5df to your computer and use it in GitHub Desktop.
Tygodniowe Wyzwanie Programistyczne - zadanie #1 dla początkujących (rozwiązanie)
package com.pietras.service;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Sandwich {
public static final String CHLEB = "chleb";
public static String getSandwichUsingRegexp(String input){
Matcher matcher = Pattern.compile("chleb(.*)chleb").matcher(input);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
public static String getSandwichUsingIndexOf(String input) {
int firstIndex = input.indexOf(CHLEB);
int lastIndex = input.lastIndexOf(CHLEB);
if (firstIndex < lastIndex) {
return input.substring(firstIndex + CHLEB.length(), lastIndex);
}
return "";
}
public static String getSandwichUsingSplit(String input){
String[] ingredients =input.split(CHLEB,-2);
if(ingredients.length>=3) {
String[] ingredientsInside = Arrays.copyOfRange(ingredients, 1, ingredients.length - 1);
return Arrays.stream(ingredientsInside).collect(Collectors.joining(CHLEB));
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment