简体   繁体   中英

String manipulation?

I have a problem where I need to output whether or not a user inputted string has duplicate sets of characters in the suffix and prefix positions within the string, if it does I must print out "ok".

if the user inputs "ermarf" it would print "not ok"

My question is how do I code this. I thought using CharSequence() method would work; but with that you would have to indicate what specific characters to look for, and that wont work for what I'm trying to do; any suggestions?

Your best bet is regular expression. See the duplicate answer here for inspiration.

How can I find repeated characters with a regex in Java

You can use a Set of Character s.

eg

String input = "abcde";
Set<Character> set = new HashSet<Character>(); 
boolean hasDup = false; 
for(int i=0; i<input.length(); i++) {
    if(!set.add(input.charAt(i))) {
        hasDup = true;
        break;
    }
}

use set of strings, and fill it reading next char of main string. then check size of set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM