Java判断一个字符串str中中文的个数,经过总结,有以下几种方法(全部经过验证),可根据其原理判断在何种情况下使用哪个方法: 1. char[] c = str.toCharArray(); for(int i = 0; i < c.length; i ++) { String len = Integer.toBinaryString(c[i]); if(len.length() > 8) count ++; } 根据一个中文占两个字节,假如一个字符的字节数大于8,则判断为中文. 2 . S
已知一个字符串#####,现需要替换偶数位置的#为&. function replaceDemo(){ var s = "1#2#3#4#5#"; var regex = /#/g; var index = 1; s=s.replace(regex,function(){index++;return index%2?'&':arguments[0]}); return s; } 注释: 1.由于需要匹配整个字符串,因此政策表达式需要添加g参数. 2.index用于记录匹
题目:给定一个字符串string,找出string中无重复字符的最长子串. 举例: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke"
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase