#Fastest way to check if an array or string contains certain characters

5 messages · Page 1 of 1 (latest)

hearty wren
#

Problem
My code is currently using a String[] array, however I am finding I need to write a foreach loop to check if it contains certain characters (I am only working with 18 nums/ops, 1...10, 25, 50, 75 100, +, -, *, /)

String[] copy = {"100", "10", "25", "9", "8", "-", "75", "-", "-", "-", "-"};

int opCheck = 0;
for (String token : copy) {
    if (token.equals("-") || token.equals("/")) {
        opCheck++;
    }
}

if (opCheck != 5) {
    postfixArray[postfixIndex++] = copy;
}

I understand String[] operations aren't the fastest, and am wondering what other ways are faster.

Possible Solutions
I have thought about possibly encoding the content, i.e.
1 = a, 2 = b....10 = j, + = o, - = p
1 = 1, 2 = 2....10 = 10, + = 11, - = 12

What would be faster to check if it contains certain characters?

String[] strArr = {"100", "10", "25", "9", "8", "-", "75", "-", "-", "-", "-"};
int[] intArr = {100, 10, 25, 9, 8, 12, 75, 12, 12, 12, 12} // FYI - = 12
char[] chArr = {'n', 'j', 'k', 'i', 'h', 'p', 'm', 'p', 'p', 'p', 'p'}
String str = "njkihpmpppp"
karmic cragBOT
#

This post has been reserved for your question.

Hey @hearty wren! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hearty wren
#

Timed it myself