用java实现小写字母转大写字母

来源:互联网 发布:知乎提问后自己找不到 编辑:程序博客网 时间:2024/04/30 07:32

写一个函数,实现从小写转换为大写

1、从键盘录入字母,从显示屏输出大写字母

public class ByteArrayTest {
public static void main(String[] args) throws Exception {

transform(System.in,System.out);

}

//读取输入流数据,然后转换成大写
public static void transform (InputStream in,OutputStream out) throws Exception {
int ch = 0 ; 
while ((ch=in.read())!=-1) {
int UpperCh = (int)Character.toUpperCase((char)ch);
out.write(UpperCh);
}
}
}


2、将一个字符串转换成大写字母

public class ByteArrayTest {
public static void main(String[] args) throws Exception {
String str = "dsadfsddsdsadsafsadsadsa";
byte[] buf = str.getBytes();

ByteArrayInputStream in = new ByteArrayInputStream(buf);

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] result = out.toByteArray();
transform(in,out);
System.out.println(new String(result));
}

//读取输入流数据,然后转换成大写
public static void transform (InputStream in,OutputStream out) throws Exception {
int ch = 0 ; 
while ((ch=in.read())!=-1) {
int UpperCh = (int)Character.toUpperCase((char)ch);
out.write(UpperCh);
}
}
}

0 0
原创粉丝点击