Java.String类

//String类 //StringBuffer类 //String类的equals()、equalsIgnoreCase()方法 //indexOf()、subString()方法 class ReadLine { public static void main(String [] args) { byte buf[]=new byte[1024]; String strInfo=null; int pos=0; int ch=0; System.out.println("Please enter info,input bye for exit:"); while(true) { try { ch=System.in.read(); //从键盘读取输入 } catch(Exception e) { System.out.println(e.getMessage()); } switch(ch) { case '/r': //回车 break; case '/n': strInfo=new String(buf,0,pos); //String类的构造方法String(buf,0,pos),把数组buf里面的值从0到pos取出,用来创建一个新的String类。此方法可以将一个字节数组转换为字符串。 if(strInfo.equalsIgnoreCase("bye")) //字符串比较。xxxXxx.equalsIgnoreCase()忽略大小写。 { return ; //结束当前域的操作。声明一个类的时候没有设置返回值(void),在该类的任意位置写下return,那么代码运行到此处就不在运行,跳出此类,继续运行下面的操作。 } else { System.out.print(strInfo); pos=0; break; } default: buf[pos++]=(byte)ch; } } } } //

你可能感兴趣的:(Java.String类)