day15-String类的API和StringBuffer类的API
来源:互联网 发布:常用mysql语句面试题 编辑:程序博客网 时间:2024/06/05 06:26
String对象的API
package cn.itcast.demo02;/* * String类的查找功能 */public class StringDemo4 {public static void main(String[] args) {function_9();}/* * boolean equals(Object obj) * 方法传递字符串,判断字符串中的字符是否完全相同,如果完全相同返回true * * boolean equalsIgnoreCase(String s) * 传递字符串,判断字符串中的字符是否相同,忽略大小写 */public static void function_9(){String str1 = "Abc";String str2 = "abc";//分别调用equals和equalsIgnoreCaseboolean b1 = str1.equals(str2);boolean b2 = str1.equalsIgnoreCase(str2);System.out.println(b1);System.out.println(b2);}/* * char[] toCharArray() 将字符串转成字符数组 * 功能和构造方法相反 */public static void function_8(){String str = "itcast";//调用String类的方法toCharArray()char[] ch = str.toCharArray();for(int i = 0 ; i < ch.length ; i++){System.out.println(ch[i]);}}/* * byte[] getBytes() 将字符串转成字节数组 * 此功能和String构造方法相反 * byte数组相关的功能,查询编码表 */public static void function_7(){String str = "abc";//调用String类方法getBytes字符串转成字节数组byte[] bytes = str.getBytes();for(int i = 0 ; i < bytes.length ; i++){System.out.println(bytes[i]);}}/* * int indexOf(char ch) * 查找一个字符,在字符串中第一次出现的索引 * 被查找的字符不存在,返回-1 */public static void function_6(){String str = "itcast.cn";//调用String类的方法indexOfint index = str.indexOf('x');System.out.println(index);}/* * boolean contains (String s) * 判断一个字符串中,是否包含另一个字符串 */public static void function_5(){String str = "itcast.cn";//调用String类的方法containsboolean b =str.contains("ac");System.out.println(b);}/* * boolean endsWith(String prefix) * 判断一个字符串是不是另一个字符串的后缀,结尾 * Demo.java * .java */public static void function_4(){String str = "Demo.java";//调用String类方法endsWithboolean b = str.endsWith(".java");System.out.println(b);}/* * boolean startsWith(String prefix) * 判断一个字符串是不是另一个字符串的前缀,开头 * howareyou * hOw */ public static void function_3(){ String str = "howareyou"; //调用String类的方法startsWith boolean b = str.startsWith("hOw"); System.out.println(b); }/* * String substring(int beginIndex,int endIndex) 获取字符串的一部分 * 返回新的字符串 * 包含头,不包含尾巴 * * String substring(int beginIndex)获取字符串的一部分 * 包含头,后面的字符全要 */public static void function_2(){String str = "howareyou";//调用String类方法substring获取字符串一部分str= str.substring(1, 5);System.out.println(str);String str2 = "HelloWorld";str2 = str2.substring(1);System.out.println(str2);}/* * int length() 返回字符串的长度 * 包含多少个字符 */public static void function(){String str = "cfxdf#$REFewfrt54GT";//调用String类方法length,获取字符串长度int length = str.length();System.out.println(length);}}
StringBuffer类的API
package cn.itcast.demo03;public class StringBufferDemo {public static void main(String[] args) {function_5();}/* * StringBuffer类的方法 * String toString() 继承Object,重写toString() * 将缓冲区中的所有字符,变成字符串 */public static void function_5(){StringBuffer buffer = new StringBuffer();buffer.append("abcdef");buffer.append(12345);//将可变的字符串缓冲区对象,变成了不可变String对象String s = buffer.toString();System.out.println(s);}/* * StringBuffer类的方法 * reverse() 将缓冲区中的字符反转 */public static void function_4(){StringBuffer buffer = new StringBuffer();buffer.append("abcdef");buffer.reverse();System.out.println(buffer);}/* * StringBuffer类方法 * replace(int start,int end, String str) * 将指定的索引范围内的所有字符,替换成新的字符串 */public static void function_3(){StringBuffer buffer = new StringBuffer();buffer.append("abcdef");buffer.replace(1, 4, "Q");System.out.println(buffer);}/* * StringBuffer类方法 insert * insert(int index, 任意类型) * 将任意类型数据,插入到缓冲区的指定索引上 */ public static void function_2(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.insert(3, 9.5); System.out.println(buffer); }/* * StringBuffer类方法 * delete(int start,int end) 删除缓冲区中字符 * 开始索引包含,结尾索引不包含 */public static void function_1(){StringBuffer buffer = new StringBuffer();buffer.append("abcdef");buffer.delete(1,5);System.out.println(buffer);}/* * StringBuffer类方法 * StringBuffer append, 将任意类型的数据,添加缓冲区 * append 返回值,写return this * 调用者是谁,返回值就是谁 */public static void function(){StringBuffer buffer = new StringBuffer();//调用StringBuffer方法append向缓冲区追加内容buffer.append(6).append(false).append('a').append(1.5);System.out.println(buffer);}}
阅读全文
0 0
- day15-String类的API和StringBuffer类的API
- API-String类和StringBuffer类
- 常用对象API(String类)和 StringBuffer
- String、Integer、StringBuffer和API类中的常用方法
- JAVA-4.2-API帮助文档、String类、StringBuffer和StringBuilder
- 重要的JAVA-API StringBuffer类
- 中文string类的API
- String\StringBuffer\StringBulid简介_包装类_Byte_Short_Integer_Long/day15
- String与StringBuffer API
- Java API(String类和StringBuffer类、System类和Runtime类)
- JAVA API系列----String类与StringBuffer类
- 黑马程序员_JAVA之API(String类与StringBuffer类)
- 黑马程序员 常用对象API-- String类、Stringbuffer、StringBuilder总结
- 黑马程序员《常用对象API string类 stringbuffer、stringBuilder》
- String类和stringBuffer类的区别
- String类和StringBuffer类的区别
- String类和StringBuffer类的区别
- String类和StringBuffer类的比较
- Android打包签名混淆发布
- getSharedPreferences 两个参数的含义
- BING: Binarized Normed Gradients for Objectness Estimation at 300fps(CVPR 2014)解读
- java程序员面试题之32问
- 'Start TomcatV8.5 Server at localhost' has encountered a problem
- day15-String类的API和StringBuffer类的API
- linux 安装ss
- jQuery中$.fn的用法示例介绍
- 腾讯云CentOS服务器上配置nginx+python3
- POJ 1338 Ugly Number 优先队列
- Redis和Memcache的区别总结
- 第五简单mybatis resulstMap-helloworld及动态sql
- 数据过滤
- 9种高性能可用高并发的技术架构