字符串-string

(1)string在Java中是一个引用类型,string变量可引用一个字符串对象

(2)

例1:

s0,s1,s2引用同一个对象

New创建的两个string是不同的对象只是内容相同。

例2:

s1,s2引用同一个字符串对象,

s1+=”b”;后s1是引用了新的对象,

s1是一个引用值(地址)不能与“ab”比较内容,即引用值不与内容匹配,

equals()方法可比较字符串内容。

(3)字符串比较

一:s1=”abcd”;s2=”abcd”;if(s1==s2)->此处比较的是s1,s2是否引用同一个对象(地址是否相同)。

二:比较字符串内容。 equals()。equalsIgnoreCase()。compareTo()字典法比较,返回值(0->相等,负数,正数)。regionMatches()比较字符串中某一部分是否相等

equals()方法:

(4):字符串查找。

查找某一字符或字串:

indexOf();

lastIndexOf();

查询是否以某字符串开始或结束:

start sWith();

endWith();

(5)常用字符串方法:

1. subString()提取字串;

例:

public class Stringx {

public static void main(String args[]) {

String s="I am a boy.";

String temp=s.substring(0,6);

System.out.println(temp);

}

}

2. concat()连接字串,或直接用“+”连接。

例:

public class Stringx {

public static void main(String args[]) {

String s="I am a";

String temp=" boy!";

System.out.println(s.concat(temp));

}

}

3. String.valueOf(),将其他类型转化为String类型。

例:

public class Stringx {

public static void main(String args[]) {

int a=12345;

String tem=String.valueOf(a);

System.out.println(tem);

}

}

4. Length();

例:

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

System.out.println(tem.length());

}

}

5. charAt();

例:

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

System.out.println(tem.charAt(3));

}

}

6. getChars();

例:

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

char ch[]=new char[10];

tem.getChars(1,5,ch,0);

System.out.println(ch[4]);

}

}

7. replace();

例:replaceAll(String,String);

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

tem=tem.replaceAll(tem, "aaaaa");

System.out.println(tem);

}

}

8. toUpperCase(),toLowerCase();

例:

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

tem=tem.toUpperCase();

System.out.println(tem);

}

}

9. trim();

例:

public class Stringx {

public static void main(String args[]) {

String tem="  aBCdef g  ";

tem=tem.trim();

System.out.println(tem);

}

}

10. toCharArray();

例:

public class Stringx {

public static void main(String args[]) {

String tem="abcdefg";

char ch[]=new char[10];

ch=tem.toCharArray();

for(int i=0;i<tem.length();i++)

System.out.print(ch[i]);

}

}

(6)StringBuffer类

String类型中s1=”abcd”,s1+=”ff”,s1前后引用两个不同的字符串对象,String对象创建后内容不可更改。

StringBuffer对象中可更高内容,长度自动改变

方法:

insert()

delete();

append();等

(7)StringBuilder类

(8)Character类

(9)StringTokenizer类

(10)String类实验:字串加密

设计思想:

设置加密/解密的key=x;

加密/解密:改变每一个字符原字符,前移(加),后移(减)key个单位。

程序流程图:

源代码:

import java.util.Scanner;

//字串加密

public class Mystic {

private String str;

public void setStr(String s) {

str=s;

}

public String getStr() {

return str;

}

public void jiami(String s) {

str=s;

for(int i=0;i<s.length();i++) {

str=str.replace(str.charAt(i),(char)(s.charAt(i)+4));

}

}

public void jiemi(String s) {

str=s;

for(int i=0;i<s.length();i++) {

str=str.replace(str.charAt(i),(char)(s.charAt(i)-4));

}

}

public static void main(String[] args) {

System.out.print("enter the string:");

Scanner scan=new Scanner(System.in);

String tem=scan.next();

Mystic mm=new Mystic();

System.out.println("choice:1->加密;2->解密。");

System.out.print("your choice:");

int ch=scan.nextInt();

if(ch==1) {

mm.jiami(tem);

System.out.println("加密后:"+mm.getStr());

}

else {

mm.jiemi(tem);

System.out.println("解密后:"+mm.getStr());

}

}

}

结果截图:

最新文章

  1. 还是俄罗斯方块之android版
  2. Gone Fishing POJ 1042
  3. The currently selected variant &quot;arm-debug&quot; uses split APKs, but none of the 1 split apks are compatible with the current device with density &quot;213&quot; and ABIs &quot;x86&quot;.
  4. Linux IPC System V 信号量
  5. mongoDB01 介绍
  6. jq 动态判断设备添加对应meta viewport属性内同
  7. redis8--数据持久化两种方式
  8. 005-docker启动设置环境变量
  9. nginx 开启静态 gzip 配合 Vue 构建
  10. CPU高速缓存
  11. sass command
  12. django 认证系统--2
  13. LCD驱动应该怎么写?–基于stm32F407 [复制链接]
  14. Java动态性之反射机制(reflection)
  15. OpenGL学习笔记(1) 画一个三角形
  16. android boot.img unpack pack
  17. FDQuery sqlserver 临时表
  18. Mac 10.12安装WebStorm
  19. lisp base
  20. 初识NuGet及快速安装使用

热门文章

  1. springboot+springcloud config
  2. (转)SC命令---安装、开启、配置、关闭windows服务 bat批处理
  3. Day3-Python基础3--局部变量和全局变量
  4. HTTP-Runoob:HTTP请求方法
  5. 用Unreal Engine绘制实时CG影像
  6. Celery-4.1 用户指南: Signals (信号)
  7. Java enum(枚举)使用详解之四
  8. mybatis(非常详细的哦~~~~)
  9. DAY7-面向对象之多态与多态性
  10. LinearLayout线性布局搭配权重属性的使用