2024-07-02 07:25:08
public static void main(String[] args) {
//用Unicode码实现
String s = "12345689我飞电风扇[],";
//找第一个汉字
for (int index = 0;index<=s.length()-1;index++){
//将字符串拆开成单个的字符
String w=s.substring(index, index+1);
if(w.compareTo("\u4e00")>0&&w.compareTo("\u9fa5")<0){// \u4e00-\u9fa5 中文汉字的范围
System.out.println("第一个中文的索引位置:"+index+",值是:"+w);
break;
}
}
//找第一个中文符号
for (int index = 0;index<=s.length()-1;index++){
//将字符串拆开成单个的字符
String w=s.substring(index, index+1);
String reg ="【。,!?】";//存放你要检测的中文符号
if(reg.indexOf(w)!=-1){//
System.out.println("第一个中文符号的索引位置:"+index+",值为:"+w);
break;
}
}
}
运行结果:
2024-07-02 01:11:39
package tool;
public class CopyCat
{
public static void main ( String[] args )
{
String string = "adf你.?的说法sdf";
String reg = "[\u4e00-\u9fa5]";
int index = -1;
if (string.matches (".*" + reg + ".*"))
{
index = string.split (reg)[0].length ();
}
System.out.println (index);
String regex = "[。,!?()《》……、:——【】;’”‘“]";
int ind = -1;
if (string.matches (".*" + regex + ".*"))
{
ind = string.split (regex)[0].length ();
}
System.out.println (ind);
}
}
2024-07-02 18:20:29
2024-07-02 02:39:32
2024-07-02 04:04:34