java查询String中所有的元素并输出重复次数 找出string数组里的重复元素并输出
java\u600e\u4e48\u67e5\u8be2\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u5e76\u4e14\u8ba1\u7b97\u4ed6\u51fa\u73b0\u7684\u6b21\u6570\u3002public class FindSubStringClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcdabcdabcdabcd";
String subStr = "abcd";
int cursor = 0, subStrLen = subStr.length(), totalStrLen = str.length();
int count = 0; // \u8868\u793a\u91cd\u590d\u5b57\u7b26\u7684\u4e2a\u6570
while ((cursor + subStrLen) <= totalStrLen) {
String tempStr = str.substring(cursor, cursor + subStrLen); // \u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32
if (tempStr.equals(subStr)) {
count++;
cursor += subStrLen;
} else {
cursor++;
}
}
System.out.println(subStr + "\u51fa\u73b0\u5728" + str + "\u4e2d\u7684\u6b21\u6570\u4e3a" + count + "\u6b21");
}
}
int i,counter[100];
for(i=0;i<=100;i++) counter[i]=0;
for(i=0;i<=strlen(string);i++) counter[string[i]-32]++;
for(i=0;i1) printf("%c ",i+32);
实现思路:就是将字符串变为一个一个的char类型,之后进行循环计数,同样的加1,之后输出计算结果。
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
String s = "abcdaebijkd";
ArrayList list = new ArrayList();
for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
if(s.indexOf(c,i+1)>-1){
Character ch = new Character(c);
if(!list.contains(ch))
list.add(ch);
}
}
for(int i=0; i<list.size(); i++)
System.out.print(list.get(i)+",");
}
}
String input ="a,ab,abc,a,b,ab,abc,abced";
TreeMap<String,Integer> stringMap = new TreeMap<String,Integer>();
String[] array = input.split(",");
for (int i = 0; i < array.length; i++) {
if(array.equals("") == false){
if(stringMap.containsKey(array[i])){
stringMap.put(array[i], stringMap.get(array[i])+1);
}else{
stringMap.put(array[i], 1);
}
}
}
Iterator<String> it = stringMap.keySet().iterator();
while(it.hasNext()){
String c = it.next();
int count = stringMap.get(c);
System.out.println("字符:"+c+",出现次数:"+count+"次");
}
绛旓細鐢string绫鐨spilt锛堬級鏂规硶浠ラ楀彿浣滀负鍒嗛殧绗﹀惂String="a,ab,abc,a,b,ab,abc,abced"鐨勬暟缁勫垎鎴"a" "ab" "abc" "a," "b""ab,""abc""abced"銆傘傘傜瓑绛夊嚑涓崟涓殑鏁扮粍锛岀劧鍚庡湪缁熻姣忎釜鐨勪釜鏁帮紝杈撳嚭鍗冲彲銆傘傘
绛旓細public class A{ public static void main(String[] args){ String match1 = "asd..zxc";String match2 = "asdzxc";String match3 = "asd @anthing@ zxc";String NOTmatch1 = "asdxc";String NOTmatch2 = "";String NOTmatch3 = "asd..zxczx";/// String regex = "^asd.*zxc$"...
绛旓細杩欎釜闂,鍙浠庢暟鎹簱鍗曚釜鏌ヨ鍑烘潵鍐嶆嫾鍑烘潵灏辫浜.select val from table where key = "a";
绛旓細String str="(12+2)*3";String result="";int a,b,c;//灏嗗瓧绗︿覆涓殑闈炴暟瀛楀瓧绗﹁浆鎹负绌烘牸 for(int i=0;i<str.length();i++){ if(Character.isDigit(str.charAt(i))){ result+=str.charAt(i);}else{ result+=' ';} } String[] arrStr=result.trim().split("\\s+");//鍘...
绛旓細杩欎釜鏄痡son瀛楃涓诧紝鍙互浣跨敤fastjson锛屽皢杩欎釜瀛楃涓茶浆鎹㈡垚jsonobject瀵硅薄锛岃瀵硅薄鏄疢AP鎺ュ彛鐨涓涓疄鐜帮紝鍙互鐢╫bj.get(key)鐨勬柟寮忚幏鍙栬瀵硅薄涓璳ey瀵瑰簲鐨勫笺
绛旓細1.妫鏌ユ暟缁勪腑鏄惁鍖呭惈鐗瑰畾鍊鐨鍥涚涓嶅悓鏂规硶 1锛変娇鐢↙ist锛歱ublic static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue);} 2锛変娇鐢⊿et锛歱ublic static boolean useSet(String[] arr, String targetValue) { Set<String> set = new HashSet...
绛旓細1锛氭弿杩 java.lang.String.contains() 鏂规硶杩斿洖true锛屽綋涓斾粎褰撴瀛楃涓插寘鍚寚瀹氱殑char鍊煎簭鍒 2锛氬0鏄 public boolean contains(CharSequence s)3锛氳繑鍥炲 姝ゆ柟娉曡繑鍥瀟rue锛屽鏋滄瀛楃涓插寘鍚紝鍚﹀垯杩斿洖false銆4锛氬疄渚 public static void main(String[] args){String str = "abc";boolean status = ...
绛旓細浠ヤ笅浠呬緵鍙傝 package com.kidd.test.zhidao;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Hello world! * */public class Main {public static void main(String[] args) {String str = "dddfdfaafffbbbtthjffcccop";String regEx = ".*a.*||.*b.*||.*c.*";...
绛旓細public static void main(String[] args) { String str="姣忔鍜屽皬鏈嬪弸鐜╂崏杩疯棌鐨勬椂鍊欙紝鎴戞绘槸绛変粬浠厛钘忓ソ锛屾垜灏卞洖瀹躲";System.out.println(str.indexOf("鍜"));System.out.println(str.indexOf("灏忔湅鍙"));stringFun('娆',str);} / 姹傚嚭c瀛楃鍦╯tr涓蹭腑鐨勪笅鏍囧苟浠ュ惊鐜殑鏂瑰紡杈撳嚭str涓 /...
绛旓細鍙互鐩存帴璋冪敤contains鏂规硶鍒ゆ柇鏄惁鍖呭惈, 渚嬪:String s = "abadc";boolean isContains = s.contains("ab");杩斿洖true琛ㄧず鍖呭惈, false琛ㄧず涓嶅寘鍚.