-
-
Save apietras/ce299f08c4d43e34f566e8d9741bc5df to your computer and use it in GitHub Desktop.
Tygodniowe Wyzwanie Programistyczne - zadanie #1 dla początkujących (rozwiązanie)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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