Results 1 to 4 of 4
- 12-09-2011, 06:58 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
Coding question to check if string contains some chars?
Hello I have below program in this program I want to check if string a contains all char in char[] b array, with the example of String a="cash" and char[] b={'c', 'h'}, I have correct result, unfortunately my string and char array will be dynamic, will change during the program and if I have String a="h" and char[] b={'c', 'h'}, my program also gives message "Yes it contains", but this is not true accoding to what I try to accomplish, I want to get "No it does not contain message". Can you guys help me?
Java Code:package stringcheck; public class StringCheck { String a="cash"; char[] b={'c', 'h'}; public StringCheck() { } public static void main(String[] args) { StringCheck mainClass = new StringCheck(); mainClass.execute(); } public void execute() { if(containsAny(a,b)) { System.out.print("Yes it contains"); } else { System.out.print("No it does not contain"); } } public static boolean containsAny(String str, char[] searchChars) { if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) { return false; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); for (int j = 0; j < searchChars.length; j++) { if (searchChars[j] == ch) { return true; } } } return false; } }
- 12-09-2011, 07:01 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Coding question to check if string contains some chars?
What are the conditions for a correct match?
- 12-09-2011, 07:22 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
Re: Coding question to check if string contains some chars?
condition is if (string value contains all character in the char array )
Let me give you more example:
Example A)
String a ="abcdef"
char[] b = {'b', 'e', 'a'}
result should be "Yes it contains"
Example B)
String a="abc"
char[] b = {'b', 'd'}
result should be "No it does not contains"
- 12-09-2011, 07:42 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Coding question to check if string contains some chars?
String method indexOf would be my choice, it searches through a String for a char. If it finds the char it returns the index position if not -1.
Use a for loop to loop through the char array calling the strings indexOf method on each char in the array. If it returns -1 you know it doesn't match.
Similar Threads
-
Problem replacing chars in String.replaceAll(), Chars such as (1/2) ½ and (1/4) ¼
By fortwnty420 in forum New To JavaReplies: 2Last Post: 07-21-2011, 03:46 AM -
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 08:22 PM -
Make String into chars
By myst in forum New To JavaReplies: 19Last Post: 06-20-2010, 04:24 PM -
Swap chars in a String?
By spatel14 in forum New To JavaReplies: 5Last Post: 06-08-2010, 09:05 PM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks