标签:
——Java培训、Android培训、iOS培训、UI培训、PHP培训、期待与您交流! ——
Scanner(InputStream source)
package com.geekds.scanner;
import java.util.Scanner;
public class ScannerTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()) {
int i = sc.nextInt();
System.out.println(i);
}else {
System.out.println("input error");
}
}
}
package com.geekds.scanner;
import java.util.Scanner;
public class NextIntTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Please input a addend:");
int a = sc.nextInt();
System.out.print(a + " + ");
int b = sc.nextInt();
System.out.print(" = " + (a + b));
}
}
package com.geekds.scanner;
import java.util.Scanner;
public class NextLineTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("username:");
String s1 = sc.nextLine();
System.out.print("password:");
String s2 = sc.nextLine();
System.out.println("username: " + s1);
System.out.println("password: " + s2);
}
}
字符串字面值”abc”也可以看成是一个字符串对象。字符串是常量,一旦被赋值,就不能被改变。
常见构造方法
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
案例演示:
package com.geekds.string;
public class StringConstruction {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] b = {43,23,8,34,90};
String str = new String(b); //解码
System.out.println(str);
byte[] b2 = {76,77,78,65,66,67,68};
String str1 = new String(b2,3,4); //从第3个索引开始解码4个
System.out.println(str1);
char[] c = {‘s‘,‘g‘,‘5‘,‘s‘};
String str2 = new String(c); //将字符数组转换成字符串
System.out.println(str2);
String str3 = new String(c,2,2);
System.out.println(str3);
}
}
package com.geekds.string;
public class StringMianshi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
mianshi1();
mianshi2();
mianshi3();
mianshi4();
String h1 = "hou";
String h2 = h1 + "li";//String 是使用StringBuilder或者StringBuffer的append方法将这俩货串联起来的
String h3 = "houli";
System.out.print(h3 == h2);
System.out.println(h3.equals(h2));
}
private static void mianshi4() {
String d1 = "d" + "o" + "n" + "g";
String d2 = "dong";
System.out.println(d1 == d2);//true,java有常量优化机制,类似于byte b = 1 + 2;
System.out.println(d1.equals(d2));//true
}
private static void mianshi3() {
String d = new String("shuai");
String h = "shuai";
System.out.println(d == h);//false
System.out.println(d.equals(h));//true
}
private static void mianshi2() {
//面试题2
String shuai = new String("dong"); //此语句创建了几个对象?
System.out.println(shuai); //常量池一份,堆内存一份
}
private static void mianshi1() {
//面试题1
String dong = "shuai"; //在常量池中创建shuai,常量池的特点是没有就创建,有就直接用
String hou = "shuai"; //直接把hou指向shuai,索引dong和hou的索引一样
System.out.println(dong == hou);
System.out.println(dong.equals(hou));
}
}
示例:
package com.geekds.string;
public class StringJudgeMethods {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
method1();
method2();
method3();
method5();
method4();
}
private static void method4() {
//判断是否为空
String ds1 = "gdsf213jp";
String ds2 = "";
//String ds3 = null;空指针异常
//空串“”和null的区别,空串“”是字符串常量,也是String类的对象,既然是对象,所以可以调用String类中的方法
//null是空常量,不能调用任何方法,否则空指针异常。null可以给任意引用数据类型赋值
System.out.println(ds1.isEmpty());
System.out.println(ds2.isEmpty());
}
private static void method5() {
String j1 = "dongshuai";
String j2 = "do";
String j3 = "ai";
System.out.println(j1.startsWith(j2));//j1是否以j2开头
System.out.println(j1.endsWith(j3));//j1是否以j3结尾
}
private static void method3() {
//contains方法,长字符串中是否包含短字符串
String l1 = "CandyHou";
String l2 = "fuck";
System.out.println(l1.contains(l2));
}
private static void method2() {
//equalsIgnoreCase()方法
String s1 = "shuai";
String s2 = "ShuaI";
System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写
}
private static void method1() {
String d1 = "dong";
String d2 = "dong";
String d3 = "Dong";
//equals()方法
System.out.println(d1.equals(d2));
System.out.println(d1.equals(d3));
}
}
package com.geekds.test;
import java.util.Scanner;
public class StringLogin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("please input username:");
String userName = sc.nextLine();
System.out.println("please input password");
String password = sc.nextLine();
//以后作比较,最好把常量放前,因为userName可能是null,通常是字符串常量调用方法,防止空指针异常
if("admin".equals(userName) && "admin".equals(password)){
System.out.println("登录成功");
break; //跳出循环
}else {
if(i == 2) {
System.out.println("you are wrong too mang times, please come back tomorrow");
}else {
System.out.println("username or password wrong! you have " + (2-i) + " chance");
}
}
}
}
}
示例:遍历字符串
package com.geekds.test;
public class StringErgodic {
/**
* 遍历字符串
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "我喜欢大长腿!";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
System.out.print(c);
//或者下面也行
System.out.print(s.charAt(i));
}
}
}
示例:统计不同类型字符个数
package com.geekds.test;
public class StringSum {
/**
* 统计字符串中各类型字符的个数
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "ASDFsdfoi235235-=,./$@%@$##$\3";
int capital = 0;
int lowerCase = 0;
int number = 0;
int other = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);//通过索引拿到每一个字符
if(c >= ‘A‘ && c <= ‘Z‘){
capital++;
}else if (c >= ‘a‘ && c <= ‘z‘) {
lowerCase++;
}else if(c >= ‘0‘ && c <= ‘9‘) {
number++;
}else {
other++;
}
}
System.out.println("capital: " + capital);
System.out.println("lower case: " + lowerCase);
System.out.println("number: " + number);
System.out.println("other character" + other);
}
}
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
String toLowerCase():把字符串转成小写。
示例:
package com.geekds.string;
import com.geekds.bean.Person;
public class TransformTest {
/**
* String类的转换功能
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
methodGetBytes();
methodToCharArray();
methodValueOf();
methodUpperLowerConcat();
}
private static void methodUpperLowerConcat() {
String s1 = "dongdaxia董大侠";
String s2 = "YuXinSuoShan";
String s3 = s1.toUpperCase();//将字符串转成大写
String s4 = s2.toLowerCase();
System.out.println(s3);
System.out.println(s4);
System.out.println(s3 + s4);//用“+”拼接能力更强大,可以将字符串与任意类型拼接
System.out.println(s3.concat(s4));//concat()方法调用和传入的都必须是字符串
}
private static void methodValueOf() {
System.out.println();
//static String valueOf()方法将任意类型转换为字符串
//底层还是用的String的构造方法完成的
char[] c = {‘d‘,‘o‘,‘n‘,‘g‘};
String s = String.valueOf(c);
System.out.println(s);
String s2 = String.valueOf(100);//将整形转换为字符串
System.out.println(s2 + 1000);
//还可以将Object转换为字符串
//valueOf()和copyValueOf()方法功能是一样的
Person p = new Person("余心所善",16);
String s3 = String.valueOf(p); //调用的是对象的toString()方法
System.out.println(p);
System.out.println(s3);
}
private static void methodToCharArray() {
System.out.println();
//将字符串转换为字符数组
// 遍历字符串有两种方式,一种是用索引,还有一种是这个方法
String s1 = "geekds";
char[] c = s1.toCharArray();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + " ");
}
}
private static void methodGetBytes() {
//将字符串转换为字节数组
String s1 = "dong";
byte[] b = s1.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
System.out.println();
/*
* 通过GBK码表将字符串转换为字节数组
* 这个过程成为编码:把人能看懂的转换为计算机能看懂的
* GBK码表一个中文代表两个字节
* GBK码表特点:中文的第一个字节肯定是负数
*/
String s2 = "静女其姝,俟我于城隅,爱而不见,搔首踟蹰。";
byte[] b2 = s2.getBytes();
for (int i = 0; i < b2.length; i++) {
System.out.print(b2[i] + " ");;
}
System.out.println();
String s3 = "琲";
byte[] b3= s3.getBytes();
for (int i = 0; i < b3.length; i++) {
System.out.print(b3[i] + " ");
}
}
}
示例:按要求转换字符,链式编程,需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
package com.geekds.test;
//练习,将一个字符串的首字母转成大写,其余小写(只考虑英文字母的大小写)
public class UpperLower {
public static void main(String[] args) {
String s = "dONGDAxia!";
//链式编程,只要保证每次调用完方法返回的是对象,就可以继续调用
String s2 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s2);
}
}
示例:把数组转成字符串
package com.geekds.test;
public class ArrayString {
public static void main(String[] args) {
int[] i = {23,200,13,4567,};
String s = "[";
for (int j = 0; j < i.length; j++) {
if(j == i.length - 1) {
s = s + i[j] + "]";
}else {
s = s + i[j] + ", ";
}
}
System.out.println(s);
}
}
String replace(char old,char new)
String replace(String old,String new)
String trim()
int compareTo(String str)
int compareToIgnoreCase(String str)
示例:
package com.geekds.string;
public class StingReplaceCompareTo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
methodReplace();
methodTrim();
methodCompareTo();
methodCompareToIgnoreCase();
}
private static void methodCompareToIgnoreCase() {
//不区分大小写的比较
String s = "dong";
String s1 = "dONG";
int i = s.compareToIgnoreCase(s1);
System.out.println(i);
}
private static void methodCompareTo() {
//按字典顺序比较两个字符串
String s1 = "geekds";
String s2 = "dongdaxia";
int i = s1.compareTo(s2);
System.out.println(i);
String s3 = "d";
String s4 = "ddd";
int i2 = s3.compareTo(s4);//字符相同,长度不同的,这样的比较长度
System.out.println(i2);
String s5 = "董";
String s6 = "猴";
int i3 = s5.compareTo(s6);
System.out.println(‘董‘ + 0);//内存中char类型的用的是ASCII码表,设置到硬盘读写的用的是跟系统相关的码表,UTF-8或者GBK
System.out.println(‘猴‘ + 0);
System.out.println(i3);
}
private static void methodTrim() {
String s = " geek ds ";
String s2 = s.trim();//去掉字符串前后两端的空格
System.out.println(s2);//用于注册的时候去掉用户名两端的空格
}
private static void methodReplace() {
//String replace(char old,char new)
String s = "dongdaxia";
String s2 = s.replace(‘o‘, ‘d‘);
String s3 = s.replace(‘z‘,‘d‘);
String s4 = s.replace("ong","ing");
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
}
直接上示例:
package com.geekds.test;
import java.util.Scanner;
public class StringReversal {
/**
* 将字符串反转
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Please input a string");
String s1 = sc.nextLine();
char[] arr = s1.toCharArray();
String s2 = "";
for (int i = arr.length - 1; i >= 0; i--) {
s2 = s2 + arr[i];
}
System.out.println(s2);
}
}
示例:
package com.geekds.test;
public class BigStringFindSmallString {
/**
* 统计大串中小串出现的次数
* 通过indexOf()方法
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String bigString = "江南可采莲,莲叶何田田,鱼戏莲叶间。鱼戏莲叶东,鱼戏莲叶西,鱼戏莲叶南,鱼戏莲叶北。";
String smallString = "莲叶";
int count = 0;//定义计数器
int index = 0;//定义索引
while((index = bigString.indexOf(smallString)) != -1) {
count++;
bigString = bigString.substring(index + smallString.length());
}
System.out.println(count);
}
}
标签:
原文地址:http://blog.csdn.net/u010941185/article/details/51714804