A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 青春是你的烟火 中级黑马   /  2015-8-2 21:13  /  387 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1、Scanner注意问题

1、Scanner中方法        
          next() -- 查找并返回来自此扫描器的下一个完整标记。
          nextLine() -- 此扫描器执行当前行,并返回跳过的输入信息。
          next() 和 nextLine() 区别:
               next()会将空格键、Tab键或Enter键等视为分隔符或结束符,不能得到带空格的字符串。
                    Scanner scanner = new Scanner(System.in);
                    String string = scanner.next();

          while(true){
               System.out.println(string);
               string = scanner.next();
          }
               nextLine()仅将Enter键作为结束符,返回Enter键前的所有字符,可以得到带空格。
2、案例
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
String str = scanner.nextLine();
System.out.println(num + str);


          1、问题描述

               当录入的顺序依次是int String,并且分别使用nextInt()和nextLine()方法获取数据的时候,会接受不到String数据     

          2、解决办法

               1、在录入完int后重新创建一个新的Scanner对象

               2、用字符串接受,然后把字符串转成int   





2、String类——构造方法

1、字符串概念

          由多个字符组成的一串数据。
2、构造方法

          创建字符串对象的几种方式

String s = new String();
通过字节数组创建字符串对象
String s = new String(byte[] bys);  
通过字节数组创建字符串对象常用
String s = new String(byte[] bys,int index, int length)
通过字节数组一部分创建字符串对象
String s = new String(char[] chs)
通过字符数组创建字符串对象常用
String s = new String(char[] chs, int index, int length);
通过字符数组一部分创建字符串对象
String s = new String(String);
通过给构造方法传入字符串创建字符字符串对象
String s = “helloworld”
直接给字符串对象赋值常用


3、字符串的特点及面试题


1、特点及注意事项
          字符串一旦被赋值,就不能改变。
          注意:字符串的值不能改变,引用是可以改变的。

2、面试题
               a:String s = new String("hello")和String s = "hello"的区别。
                          答:new String(“hello”)在内存中创建了1个或两个对象,为什么..

                                   “hello”在内存中创建了0个或一个对象,为什么…

               b:请写出结果:
                    String s1 = new String("hello");
                    String s2 = new String("hello");
                    System.out.println(s1==s2);
                    System.out.println(s1.equals(s2));

                    String s3 = new String("hello");
                    String s4 = "hello";
                    System.out.println(s3==s4);
                    System.out.println(s3.equals(s4));

                    String s5 = "hello";
                    String s6 = "hello";
                    System.out.println(s5==s6);
                    System.out.println(s5.equals(s6));

             c : ""和null的区别

                 最本质的区别是否在内存中开辟内存空间,"'会开辟内存空间,而null不会,在开发的时候要养成良好的习惯用null


4、String类——成员方法


1、判断功能

boolean equals(Object obj)
判断字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str)
判断字符串的内容是否相同,忽略大小写
boolean contains(String str)
判断字符串对象是否包含给定的字符串
boolean startsWith(String str)
判断字符串对象是否是以给定的字符串开始
boolean endsWith(String str)
判断字符串对象是否是以给定的字符串结束
boolean isEmpty()
判断字符串对象是否为空,注意是数据


2、获取功能

int length()
获取字符串长度
char charAt(int index)
返回字符串中给定索引处的字符
int indexOf(int ch)
返回指定字符在此字符串中第一次出现的索引
int indexOf(String str)
返回指定字符串在此字符串中第一次出现的索引
int indexOf(int ch,int fromIndex)
返回指定字符在此字符串中第一次出现的索引,从指定位置开始
int indexOf(String str,int fromIndex)
返回指定字符串在此字符串中第一次出现的索引,从指定位置开始
String substring(int start)
截取字符串,返回从指定位置开始截取后的字符串,原字符串长度不变
String substring(int start,int end)
截取字符串,返回从指定位置开始到指定位置结束,截取后的字符串原字符串长度不变
//如果start=0,end=该字符串的长度,那么返回的是原对象,否则返回新对象


3、转换功能

byte[] getBytes()
把字符串转换成字节数组
char[] toCharArray()
把字符串转换成字符数组
static String copyValueOf(char[] chs)
把字符数组转换成字符串
底层调用new String(char[] chs)
static String valueOf(char[] chs)
把字符数组转换成字符串
底层调用new String(char[] chs)
static String valueOf(任意类型 变量名)
把任意类型转换成字符串
String toLowerCase()
把字符变成小写原字符串不变
String toUpperCase()
把字符编程大写原字符串不变
String concat(String str)
字符串链接原字符串不变

4、其他功能

String replace(char oldChar,char newChar)
替换功能原字符串不变
String replace(String oldString,String newString)
替换功能原字符串不变
String[] split(String str)
切割功能原字符串不变
字符串的split方法中的字符串
String trim()
去除字符串两端空格原字符串长度不变
int compareTo(String str)
字典顺序比较功能
int compareToIgnoreCase(String str)
字典顺序比较功能


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马