1、String类的构造方法

(1)String(String original)  //把字符串数据封装成字符串对象

(2)String(char[] c)   //把字符数组的数据封装成字符串对象

(3)String(char[] c, int index, int count)  // 把字符数组中的一部分数据封装成字符串对象

示例:

 public class Demo01 {
public static void main(String[] args) { String str01=new String("hello"); char[] c1=new char[]{'h','e','l','l','o'};
String str02=new String(c1); char[] c2=new char[]{'h','e','l','l','o','w','o','r','l','d'};
String str03=new String(c2,0,5); System.out.println(str01);
System.out.println(str02);
System.out.println(str03); }
}

输出位:

hello
hello
hello

2、String类常用方法

(1)public boolean equals(Object obj)

将此字符串与指定的对象比较。若内容相等,返回true,否则,返回false。

equal 和 == 的区别:

  equal:比较的是值是否相同

  ==:比较的是地址值是否相同

示例:

 public class demo01 {

     public static void main(String[] args) {
String s1="helloworld";
String s2="hello";
String s3="world";
s2=s2+s3; boolean aBoolean1=s1==s2;
boolean aBoolean2=s1.equals(s2);
System.out.println("s1:"+s1+" s2:"+s2);
System.out.println(aBoolean1);
System.out.println(aBoolean2);
}
}

输出结果为:

s1:helloworld   s2:helloworld
false
true

(2)public boolean equalIgnoreCase(String otherString)

将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。

示例:

 public class demo02 {
public static void main(String[] args) {
String s1="hello world!";
String s2="Hello World!"; boolean aBoolean1=s1.equals(s2);
boolean aBoolean2=s1.equalsIgnoreCase(s2); System.out.println("s1:"+s1+" s2:"+s2);
System.out.println("s1.equal(s2):"+aBoolean1);
System.out.println("s1.equalIgnoreCase(s2):"+aBoolean2);
}
}

输出结果为:

s1:hello world!  s2:Hello World!
s1.equal(s2):false
s1.equalIgnoreCase(s2):true

(3)public boolean startsWith(String str)

判断字符串对象是否以指定的str开头

示例:

 public class Demo04 {
public static void main(String[] args) {
String s="hello world!"; boolean b1=s.startsWith("helo");
boolean b2=s.startsWith("hell"); System.out.println(b1);
System.out.println(b2);
}
}

输出为:

false
true

(4)public boolean endsWith(String str)

判断字符串对象是否以指定的str结尾

示例:

 public class Demo05 {
public static void main(String[] args) {
String s="hello world!"; boolean b1=s.endsWith("ld!");
boolean b2=s.endsWith("d!"); System.out.println(b1);
System.out.println(b2);
}
}

输出为:

true
true

(5)public int length()

获取字符串的长度,其实也就是字符个数

(6)public char charAt(int index)

获取指定索引处的字符

(7)public int indexOf(String str)

获取str在字符串对象中第一次出现的索引

示例:

 public class Demo06 {
public static void main(String[] args) {
String str="hello world! this is me"; System.out.println("字符串\"hello world! this is me\"的长度为:"+str.length()); char c=str.charAt(2);
System.out.println("位于字符串str,索引为2的字符为:"+c); int index=str.indexOf("ll");
System.out.println("\"ll\"在字符串str的索引位置是:"+index); }
}

输出为:

字符串"hello world! this is me"的长度为:23
位于字符串str,索引为2的字符为:l
"ll"在字符串str的索引位置是:2

(8)public String substring(int start)

从start开始截取字符串

(9)public String substring(int start,int end)

从start开始,到end结束截取字符串。包括start,不包括end

(10)public char[] toCharArray()

把字符串转换为字符数组

(11)public String toLowerCase()

把字符串转换为小写字符串

(12)public String toUpperCase()

把字符串转换为大写字符串

(13)public String trim()

将去除字符串两端空格

(13)public String[] split (String str)

按照指定符号分割字符串

最新文章

  1. 获取文件mime类型
  2. WinForm GDI+ 资料收集
  3. 【jmeter】属性和变量
  4. 如何修正导入模型的旋转? How do I fix the rotation of an imported model?
  5. SCAU 10893 Spiral
  6. Android之打开闪光灯关键代码
  7. Android ANR、Force Closed(转)
  8. 关于Opencv2.4.x中stitcher类的简单应用
  9. 波浪号和Hyphen扩展
  10. UVa12563- Jin Ge Jin Qu hao
  11. mysql中 union是什么鬼
  12. python摸爬滚打之day33----线程
  13. 让我对 docker swarm mode 的基本原理豁然开朗的几篇英文博文
  14. centos6.5报错:checking filesystems failed问题处理
  15. Spring Boot Actuator RCE
  16. dblinks
  17. 浅谈jvm
  18. <%@ include file="">和<jsp:include file="">区别
  19. Hadoop相关笔记
  20. oracle 不能是用变量来作为列名和表名 ,但使用动态sql可以;

热门文章

  1. oracle11g数据库导入、导出操作
  2. Angular 从入坑到挖坑 - 路由守卫连连看
  3. 【Mybatis】mybatis开启Log4j日志、增删改查操作
  4. svg高级应用及动画
  5. Java实现 LeetCode 299 猜数字游戏
  6. Java实现 LeetCode 132 分割回文串 II(二)
  7. Java实现 蓝桥杯VIP 算法提高 三角形面积
  8. python自学Day06(自学书籍python编程从入门到实践)
  9. JAVA第三次blog总结
  10. iOS-Reactive Cocoa的常见用法