java string 分析_java中的string分析

Object 类 顶级

toString()方法

一般都要重写

返回对象文本描述

被很多API调用

==

比较变量的值,引用变量的值是地址值,引用变量==比较的是地址值

不能用于比较逻辑上对象是否相等

equals 方法

一般要重写(重写时也要重写 hashCode方法)

Object的equals 方法用于对象的相等 逻辑

instanceof  java的运算符

测试一个对象是否是一个类的实例

A instanceof B

对象A是否是类型B的实例  返回boolean

String 类 重要类

final类

有很多的优化

底层就是:字符数组

字符串是有字符数组组成的。

字符串对象不可改变

字符串引用是可以重新赋值

字符串的连接底层是字符数组复制实现的

String API

API方法不会改变原字符串对象

返回值与原有字符串不同时候是新对象

返回值与原有字符串一样的时候还是引用之前的对象

为了性能

常量池(提高性能)

静态字符串(字面量/常量/常量连接的结果)在静态池中创建,很多

重用静态字符串

String  toLowerCase()            返回字符串小写形式

toUpperCase()           返回字符串大写形式

trim()              返回字符串为原始字符串去掉前后的空白

(空格 \t \n \r)

endsWith("str")         字符串结尾是否有str

startsWith("str")        字符串开头是否有str

length()            返回字符串的长度

indexOf()  有重载      字符串中查找别一个字符串

substring()          截取字符串中的一部分

indexOf()和substring()     配合使用可以截取特定的字符串

package day022;

public class Demo01 {

public static void main(String[] args){

String s1 = "Hello";

String s2 = s1;

s1 = s1 + "World";

System.out.println(s1);

System.out.println(s2);

System.out.println(s1.toLowerCase());

}

}

package day22;大写转小写,小写转大写

import java.util.Arrays;

public class Demo02 {

public static void main(String[] args) {

char[] chs = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd',

'!' };

char[] chs1 = Arrays.copyOf(chs, chs.length);

for (char c : chs) {

if (c > 64 && c 

c = (char) (c + 32);

}

System.out.print(c);

}

for (char c : chs) {

if (c > 'a' && c 

c = (char) (c - 32);

}

System.out.print(c);

}

}

}

package day22;// 大写转小写同时小写转大写

import java.util.Arrays;

public class Demo03 {

public static void main(String[] args){

char[] chs = {'H','e','l','l','o',' ','W','o','r','l','d','!'};

char[] chs1 = Arrays.copyOf(chs, chs.length);

for(char c:chs){

if(c>64 && c<91){

c = (char) (c + 32);

System.out.print(c);

continue;

}else if(c>'a' && c

c = (char) (c - 32);

}

System.out.print(c);

}

}

}

package day22;//字符串去掉前后的空白

public class Demo04 {

public static void main(String[] args){

String name = " \n lmd tx \t \r  ";

System.out.println(name.trim());

}

}

package day22;

public class Demo05 {

public static void main(String[] args){

String name = " \n lmd tx \t \r  ";

System.out.println(name.trim());

String file = "p.png";

boolean isPng = file.endsWith(".png");//字符串结尾是否有.png

System.out.println(isPng);

boolean isP = file.startsWith("p");//字符串开头是否有p

System.out.println(isP);

}

}

package day22;

public class Demo06 {

public static void main(String[] args){

String str = "asdfzcvqewrfgHHajlsdjf";

int index = str.indexOf("HH");//找到返回序号

System.out.println(index);

index = str.indexOf("GG");//找不到返回-1

System.out.println(index);

index = str.indexOf('a');//从头开始

System.out.println(index);

index = str.indexOf('a', 13);//从指定位置开始找

System.out.println(index);

System.out.println(str.substring(4,6));// 截取字符串中的一部分

}

}

package day22;

public class Demo07 {//特殊截取

public static void main(String[] args){

String email ="81240805@qq.com";

String QQ = email.substring(0,email.indexOf("@"));//从邮箱中截取QQ号

System.out.println(QQ);

String domain = email.substring(email.indexOf("@")+1);//从邮箱中截取域名

System.out.println(domain);

}

}

package day22;

public class Demo08 {//条码应用 EAN-13

public static void main(String[] args){

String str = "6922711076348";

System.out.println(str);

char c1;

char c2;

int n=0;

int m=0;

for(int i = 0;i 

c1 = str.charAt(i);

n = n +c1-'0';

System.out.print(c1);

}

System.out.println();

for(int i = 1;i 

c2 = str.charAt(i);

System.out.print(c2);

m = m +c2-'0';

}

System.out.println();

System.out.println(n);

System.out.println(m);

int sum = n + m*3;

System.out.println(sum);

int q =(10-sum%10)%10;

if(q == 0){

System.out.println(0);

}else{

System.out.println(q);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值