Skip to content
Snippets Groups Projects
Commit 609622b2 authored by aichinos's avatar aichinos
Browse files

haku metodi tehdy demossa

parent a46c66e4
No related branches found
No related tags found
No related merge requests found
package apumetodit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author aichi
* @version 27 Feb 2025
*
*/
public class Haku {
/**
* @param jono mistä etsitään haku-sanaa
* @param haku hakusana
* @return true, jos haku-string löytyy jonossa
* @example
* <pre name="test">
* String jono = "kissa";
* String haku1 = " k";
* String haku2 = " ";
* String haku3 = "SS";
* onkoSamat(jono, haku1) === true;
* onkoSamat(jono, haku2) === false;
* onkoSamat(jono, haku3) === true;
*/
public static boolean onkoSamat(String jono, String haku){
String tasta = jono.trim().toLowerCase();
String regex = haku.trim().toLowerCase();
if (tasta.length() == 0 || regex.length() == 0) return false;
//System.out.println("."+tasta+".");
//System.out.println("."+regex+".");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(tasta);
return matcher.find();
}
/**
* @param args ei käytössä
*/
public static void main(String[] args)
{
if (onkoSamat("Matti Nykänen", " matti* ")) System.out.println("Samat on!");
else System.out.println("Eivät ole.");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment