import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.junit.Test;
public class StringDemo {
@Test
public void demo1(){
String s="1234567";
System.out.println(s.length());
System.out.println(s.charAt(2));
System.out.println(s.getBytes().length);
}
@Test
public void demo2() throws UnsupportedEncodingException{
String s="1234567中";
System.out.println(s.length());
System.out.println(s.charAt(2));
System.out.println( s.getBytes("gbk").length);
}
@Test
public void demo3() throws IOException{
String s="1234567er汉erer";
if(s.length()<s.getBytes("gbk").length){
System.out.println("有汉字");
}else{
System.out.println("无汉字");
}
}
@Test
public void demo4() throws IOException{
String s="1FK236W7er汉et34中国fg语法23Rr45er";
for(int i=0;i<s.length();i++ ){
char ch = s.charAt(i);
int len = (""+ch).getBytes().length;
if(len>1){
System.out.println("汉字:"+ch);
}else if(ch>='0'&&ch<='9'){
System.out.println("数字:"+ch);
}else{
System.out.println("字母:"+ch);
}
}
}
@Test
public void demo4_2() throws IOException{
String s="1FK236W7er汉et34中国fg语法23Rr45er";
for(char ch: s.toCharArray() ){
int len = (""+ch).getBytes().length;
if(len>1){
System.out.println("汉字:"+ch);
}else if(ch>='0'&&ch<='9'){
System.out.println("数字:"+ch);
}else{
System.out.println("字母:"+ch);
}
}
}
@Test
public void demo5() throws IOException{
String s="1FK236W7er汉et34中国fg语法23Rr45er";
s=s.toUpperCase();
System.out.println(s);
s=s.replace('R', '8');
System.out.println(s);
String str="Jack,20,Tom,22,张三,30";
String ss[] = str.split(",");
for(String a:ss){
System.out.println(a);
}
}
@Test
public void demo6(){
String str="Jack,20 Tom 22|张三,30";
str=str.replace('|', ',');
String ss[] = str.split(",| +");
for(String s:ss){
System.out.println(s);
}
}
}
import java.util.Scanner;
import org.junit.Test;
public class StringDemo2 {
@Test
public void demo(){
String str1="abc123";
String str2="abcdswehhw123";
int res = str1.compareTo(str2);
if(res>0){
System.out.println(str1+"更大!");
}else if(res<0){
System.out.println(str1+"更小!");
}else{
System.out.println(str1+"等于"+str2);
}
}
@Test
public void demo2(){
String str1="abc12de2323io883";
int pos = str1.indexOf('c');
System.out.println("pos:"+pos);
pos = str1.indexOf("io88");
System.out.println("pos:"+pos);
}
@Test
public void demo3(){
String names[]={"张飞","张飞丰","张无忌","赵敏","赵子龙","飞龙张","小李飞刀"};
for(String nm:names){
if( nm.indexOf("飞") >= 0 ){
System.out.println(nm);
}
}
System.out.println("===============");
for(String nm:names){
if( nm.indexOf("张") == 0 ){
System.out.println(nm);
}
}
System.out.println("===============");
for(String nm:names){
if( nm.startsWith("张") ){
System.out.println(nm);
}
}
}
@Test
public void demo4(){
String names[]={"aa.java","bc.java.c","1.java","赵敏.txt","赵子龙.java","飞龙张.c.java","小李.java.飞刀"};
for(String nm:names){
if(nm.endsWith(".java")){
System.out.println(nm);
}
}
}
@Test
public void demo5(){
String name="Jack";
String pwd="1234";
Scanner sc = new Scanner(System.in);
while (true) {
String nm = sc.nextLine();
String pwd2 = sc.nextLine();
if (nm.trim().equalsIgnoreCase(name) && pwd2.equals(pwd)) {
System.out.println("登录成功....");
} else {
System.out.println("登录失败....");
}
}
}
}