How to check if a string contains the characters from another string in Java? -
this question has answer here:
i using java 8. suppose have 2 strings a=helloworld
, b=howareyou
respectively. want find out matching characters of string b string along position in b. in example matching characters h
,e
,o
,w
,r
, r
5th character in string b. don't want find if substring or not. want check if characters of 1 string present in string or not. there way that? below java code.
public class charcheck { public static void main(string[] args) { string = "helloworld"; string b = "howareyou"; char st = containsany(a, b); system.out.println(st+", "); } private static char containsany(string str1, string str2) { if (str1 == null || str1.length() == 0 || str2 == null || str2.length() == 0) { if (str1 == null || str1.length() == 0) { return 'x'; } } (int = 0; < str1.length(); i++) { char ch = str1.charat(i); (int j = 0; j < str2.length(); j++) { if (str2.charat(i) == ch) { return ch; } } } return 'x'; } }
im not going implement solution i'll give pusedocode.
for each letter1 in string1 each letter2 in string2 if letter1 letter2 print position
so know need nested loop , comparison, becomes bit more complicated if want unique duplicates i'll leave rest
Comments
Post a Comment