JAVASE之String(一)

核心JDK API

字符串操作,集合操作、文件操作、输入输出操作、网络操作、多线程等等

这里写图片描述

包最主要的功能:归类

文档注释规范:

1、以/*开始 ,以/结束
2、加载类和方法的开头,用于说明作者,事件,版本
要实现功能的详细描述等信息
3、通过javadoc工具,可以轻松的将此注释转换为HTML文档说明;学习者和程序员主要通过文档了解API的功能
4、文档注释不同于普通的注释(//….或// ),普通注释写在程序之中,用于程序员进行代码维护和交流,无法通过工具生成文档;而文档注释(/*…/)写在类和方法的开头,专门用于生成供API使用者进行参考的文档资料

javadoc

写/** 然后回车就会出现下面这样,可以写文档注释

/**
*
* @author Administrator
*
*/
这里写图片描述

这里写图片描述

  • @param args 该参数是用来获取命令行传递的信息

常量 要求定义为大写

如:
public static final int CONST_FIELD =1;

这里写图片描述

打开

这里写图片描述

String:

这里写图片描述

String是不可变对象
java.lang.String使用了final修饰,不能被继承;
字符串底层封装了字符数组及针对字符数组的操作算法;
字符串一旦创建,对象永远无法改变,但字符串引用可以重新赋值
java字符串在内存中采用Unicode编码方式,任何一个字符对应两个字节的定长编码

定义了字符串,这个字符串内部就不会改变了。
改变的话会产生新的对象。

如:
String str = “hello”;
str = str+”!”;

String可以不用new,相当于基本类型。

如果创建了一个“hello”,再创建一个字符串也是“hello”.
但是虚拟机会先检查是否已经有hello,有的话直接指向已有的“hello”

没有的话就创建对象,也就是说那个加!的就是会新创建的对象;

双等于比的值

str 其实是一个地址
String str1 = “hello”;
String str2 = “hello”
str1==str2 true;
str1= str1+”!”;
string str3 = “hello!”

str1 == str3 !!!!是 true;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
java编译器会一个表达式两边都是字面量的话
那么编译的过程中就会进行计算,将结果生成在表达式的位置,可以节省运算时的开销。
即:
“123”+”hello” 变成 “123hello”
也就是str1等于str3!!!!!!

字面量就是:如 String str = “123” 或者 int a = 3;

new表示创建新对象,只有使用字面量赋值才会使用常量池中的对象进行重用

以下与 “123hello”比较
str4 = 1+23+”hello” true
str5 = “1”+23+”hello” true
str6 = 1+”23”+”hello” true
str7 = 1+’2’+”3”+”hello” false
因为7的 1+’2’ 不是字符是2的ascii码与1想加
str8 = new String(“123hello”);
8就是返回false ,因为这个是就是直接创建个新对象

这里写图片描述

indexOf()函数

搜索给定字符串/字符的位置,返回的下标!!

public static void main(String[] args){
    //           0123456789012345
    String str ="thinking in java";
    int index = str.indexOf("java");
    System.out.print("index:"+index);
}

返回12,从0开始。空格也算字符!

这里写图片描述

两个参数的indexOf

第一个还是要搜索的字符
第二个是从哪个下标开始搜索

public static void main(String[] args){
    //           0123456789012345
    String str ="thinking in java";
    int index = str.indexOf("java");
    System.out.println("index:"+index);
    //从第4个开始的话,就跳过了第一个in,然后继续寻找~~~
    index = str.indexOf("in",3);
    System.out.println("index:"+index);
    //从第三个开始,那么in的位置还是2,还是从第一个字符开始
    index = str.indexOf("in",2);
    System.out.println("index:"+index); 
}

这里写图片描述

还有个lastIndexOf(“”);

这个是寻找最后一次出现的位置

使用substring获取子串

substring方法用于返回一个字符串的子字符串

substring常用重载方法:

这里写图片描述

如:

public static void main(String[] args){
    //            01234567890123456789
    String str = "http://www.baidu.com";
    //取baidu ,含头不含尾
    String sub = str.substring(11,16);
    System.out.println(sub);

}  

返回baidu

写11的话就只要11后面的了,在写个数字就是尾到哪

截取域名:

public static void main(String[] args){
    //            01234567890123456789
    String str = "http://www.baidu.com";
    //取域名
    //用indexof取第一个点的位置然后加1就是域名的开始第一个
    int index1 = str.indexOf(".")+1;
    //取第一个点后的第二个点,也就是传入两个参数,第一个还是点,第二个是第一个点的位置,从他之后开始找
    int index2 = str.indexOf(".",index1)+1;
    //用头尾的index截取域名
    String sub = str.substring(index1,index2);
    System.out.println(sub);

}

trim()去两边的空白的方法

public class StringDemo5 {
public static void main(String[] args){
//空白是占着位置但是看不见,如tab等
String str = ” Hello World “;
System.out.println(str);
String trim = str.trim();
System.out.println(trim);
}

这里写图片描述

比如说输入用户名的时候,前面和后面有空格可以用trim来去除空格

charAt方法

给位置找字符,传入位置,返回字符

这里写图片描述

检测回文:

    str ="上海自来水来自海上";
    //检测回文
    //01 234 567 8
    //上海自来水来自海上
    //循环判断
    //正数位置上的字符与倒数位置上的字符都一样
    for(int i=0;i<str.length()/2;i++){
        if(str.charAt(i)!=str.charAt(str.length()-1-i)){            
        System.out.println("不是回文!");
        return;

        }
    }

System.out.println(“是回文!”);
}

在与用charAt,传位置获取值。然后是用return直接结束这个函数

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = “Thinking In Java”;
boolean as = str.startsWith(“Thi”);
boolean zx = str.endsWith(“ava”);
if(as){System.out.println(“开头匹配Thi”);}
if(!as){System.out.println(“开头不匹配Thi”);}
if(zx){System.out.println(“结尾匹配ava”);}
if(!zx){System.out.println(“结尾不匹配ava”);}
}

这里写图片描述

大小写变换

这里写图片描述

oUpperCase(); 转为全大写

tolowerCase(); 转为全小写

检测验证码:

public static void main(String[] args) {
    // TODO Auto-generated method stub
        String s ="WA45yZde";
        System.out.println("验证码:"+s);
        System.out.println("请输入验证码");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        s=s.toUpperCase();
        input = input.toUpperCase();
        if(s.equals(input.toUpperCase())){
            System.out.println("输入正确");
        }
        else{
            System.out.println("输入错误");

        }
}

这里写图片描述

做随机验证码

随机 char(random.nextInt(26)+’a/A’)
随机出数字,然后加字母就变成ascii码转换为char~~~

        Random random = new Random();
        char str9= (char) (random.nextInt()+'a');
        char char1= (char)Integer.parseInt(String.valueOf(random.nextInt(10)+'a'));
        System.out.println(char1);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值