第十一课 常用字符串函数
来源:互联网 发布:汉子笔顺数据库 编辑:程序博客网 时间:2024/06/07 14:46
1. String类的特殊用法
String s1=new String("Test");//看到new关键字就会重新分配空间
String s2=new String("Test");
//字符串池 执行到String s3="Test"; 给s3 分配空间 放到字符串池当中
//但执行到String s4="Test"; 到字符串池中检查 是否有 "Test" 如果有
//就指向已存在的地址 如果没有就分配空间
String s3="Test";
String s4="Test";
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
System.out.println(s3==s4);//true
System.out.println(s1==s4);//false
System.out.println(s1.equals(s4));//true
s4=null;
//false 一个常量和一个变量使用equals时把常量放前边
System.out.println("Test".equals(s4));
System.out.println(s4.equals("Test"));//抛出NullPointerException
2.String 一些常用方法
1.char charAt(int index):返回指定索引位置的字符
// String str="sunjava sunjava";
// char c=str.charAt(3);//索引从0开始
// System.out.println(c);
2. int length():返回字符串的长度
// String str="sunjava sunjava";
// System.out.println(str.length());
3. int indexOf(String str):返回子字符串在此字符串中第一次出现的索引位置
// String str="sunjava sunjava";
// System.out.println(str.indexOf("java"));
4. int indexOf(String str,int fromIndex):
// 返回子字符串在此字符串中第一次出现的索引位置 从指定索引fromIndex开始
// System.out.println(str.indexOf("java",5));
5. int lastIndexOf(String str):返回子字符串在此字符串中最后一次出现的索引位置
// System.out.println(str.lastIndexOf("java"));
6. boolean equals(Object obj): 比较两个字符串的内容是否相等
// String t1="Action";
// String t2="action";
// System.out.println(t1.equals(t2));
7. boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相等 忽略大小写
// System.out.println(t1.equalsIgnoreCase(t2));
8.String replace(char oldChar ,char newChar):
// 把此字符串中 oldChar字符 替换成newChar字符
// String strOld = "abcadcdeedd";
// String strNew = strOld.replace('d', 'j');
// System.out.println(strNew);
9.boolean startWith(String str) 测试此字符串是否以str开头
// String str="open hello world";
// System.out.println(str.startsWith("open"));
10. String toUpperCase():把此字符串全部转成大写
11. String toLowerCase():把此字符串全部转成小写
// String t="WebLogic 9.0";
// String tlower=t.toLowerCase();
// String tupper=t.toUpperCase();
// System.out.println(tlower);
// System.out.println(tupper);
12 .String substring(int startIndex):取字串 从索引是startIndex开始 到最后
// String str="0123456789";
// String sub1=str.substring(2);
// System.out.println(sub1);
13 .String substring(int startIndex,index endIndex)
// :取子串 从索引是startIndex开始 到endIndex之间的字符 不包括 endIndex位置的字符
// String sub2=str.substring(2,7);
// System.out.println(sub2);
14 .String trim(); 返回字符串的副本,忽略前导空白和尾部空白。
// String buffer=" save /n";
// String bufferTrim=buffer.trim();
// System.out.println(buffer.length());
// System.out.println(bufferTrim.length());
15. String[] split(string regex):根据给定的符号进行分割字符串
// String str = "adc,....yuuuudd,eeee,aaaaammmmm;;;;;;,oooo";
// String[] arr = str.split(",");
// for (int i = 0; i < arr.length; i++) {
// System.out.println(arr[i]);
// }
- 第十一课 常用字符串函数
- 第十一章 字符串与字符串函数
- 第十一章 字符串和字符串函数
- 第十一章 字符串和字符函数
- 第十一章 字符串和字符串函数(练习)总结 140606
- 字符串__常用字符串函数
- 字符串以及字符串常用函数
- PHP第八课 字符串拆分常用函数
- 第19课 Python字符串常用函数
- SQL常用字符串函数
- SQL常用字符串函数
- 常用SQL字符串函数
- 常用SQL字符串函数
- SQL常用字符串函数
- SQL常用字符串函数
- VB常用字符串函数
- 字符串操作常用函数
- 常用字符串函数
- 第十课 数组
- zz C语言的5种存储类以及关键字volatile、restrict
- 用于运行 ASP.NET 的进程帐户必须具有对 IIS 元数据库
- linux下gdb单步调试(上)
- JS框架 本人在工作中总结出来的
- 第十一课 常用字符串函数
- Windows Via C/C++ 读书笔记 9 Windows Memory Architecture
- linux下gdb单步调试(中)
- linux下gdb单步调试(下)
- Android 开发一个Activity 启动另一个Activity碰到的一个问题
- 语言模型
- 今天我很渺小,明天我一定要成为一颗闪亮的明星!!!
- n++ / ++n / n+=1 / n=n+1 实际效率比较
- 我