Saturday, 15 September 2012

java - How can I look for two specific characters in a string? -



java - How can I look for two specific characters in a string? -

string abc = "||:::|:|::";

it should homecoming true if there's 2 | , 3 : appearances. i'm not sure how utilize "regex" or if it's right method use. there's no specific pattern in abc string.

using regex bad idea, if there's no specific order them. create function counts number of times character sppears in string, , utilize that:

public int count(string base, char tofind) { int count = 0; char[] haystack = base.tochararray(); (int = 0; < haystack.length; i++) if (haystack[i] == tofind) count++; homecoming count; } string abc = "||:::|:|::"; if (count(abc,"|") >= 2 && count(abc,":") >= 3) { //do code here }

java string design-patterns char

No comments:

Post a Comment