String字符串常用API

    技术2022-07-11  102

    这里写目录标题

    什么是JDK API文档注释规范字符串String以及常用的APIString常量池 String常用APIStringBuilderStringBuffer

    什么是JDK API

    JDK中包含大量打API类库,所谓API(Application Programming Interface,应用程序编程接口)就是一些已写好.可供直接调用的功能(在Java语言中,这些功能以类的形式封装)JDK API包含的类功能强大,经常使用的有:字符串操作.集合操作.文件操作.输入输出操作.网络操作.多线程等等.

    文档注释规范

    以/** 开始,以 */结束在类和方法的开头,用于说明作者,时间,版本,要实现的详细描述信息;通过javadoc工具,可以轻松的将此注释转换为HTML文档说明;文档注释不同于普通的注释(//…或/ * … /),普同注释写在程序之中,用于程序员进行代码维护和交流,无法通过写工具生成文档;而文档注释(/**…*/)写在类和方法的开头,专门用于生成API使用者进行参考的文档资料

    字符串

    String以及常用的API

    String是不可改变的对象,用final修饰,不能被继承字符串底层封装了字符数组及针对字符数组的操作算法字符串一旦创建,对象永远无法改变,但是字符串引用可以重新赋值java字符串中任何一个字符对应16位(两个字节)的定长Unicode编码

    String常量池

    java为了提高性能,静态字符串(字面量/常量/常量连接的结果)在常量池中创建,并尽量使用同一个对象,重用静态字符串;对于重复出现的字符串直接量,JVM会首先在常量池中查找,如果存在即返回该对象/*测试String常量池/ public void testConstantPool() { String str1 = "Hello"; //不会创建新的String对象,而是使用常量池中已有的"Hello". Stringstr2 = "Hello"; System.out.println(str1 == str2); // 输出? //使用new关键字会创建新的String对象 String str3 = new String("Hello") System.out.println(str1 == str3); //输出? }

    String常用API

    使用indexOf实现检索

    indexOf方法用于实现在字符串中检索另外一个字符串 String提供几个重载的indexOf方法 1). **int indexOf(String str)在字符串中检索str,返回其第一次出现的位置,如果找不到则返回-1 2). int indexOf(String str,int fromIndex)**从字符串的fromIndex位置开始检索String还定义有lastIndexOf方法 1). **int lastIndexOf(String str,int form)**str在字符串中多次出现时,将返回最后一个出现的位置 public void testIndexOf() { Stringstr = "I can because i think i can"; int index = str.indexOf(" can"); System.out.println(index); // 2 index = strlastIndexOf(" can"); System.out.println(index); // 24 index = str.indexOf("can", 6); System.out.println(index); // 24 index = str.indexOf(" Can"); System.out.println(index); // -1 substring获取字符串 substring方法用于返回一个字符串的子字符串 substring常用重载方法定义如下 1).**String substring(int beginIndex,int endIndex)**返回字符串中从下标beginIndex(包括)开始到endIndex(不包括)结束的子字符串 2).String sunstring(int beginIndex)返回字符串从下标beginIndex开始到字符串结尾的子字符串 public void testSubstring() { Stringstr = "http://www.oracle.com"; String subStr = str.substring(11, 17 ); System.out.println(subStr); // oracle subStr = str.substring( 7 ); System.out.println(subStr); // www.oracle.com } **trim()**去掉一个字符串前后空白 public void testTrim() { String userName =" good man " userName = userName.trim(); System.out. println( userName.length ( ) ); // 8 Syste m. out. println(userName); // good man } **cahrAt(int index)**返回字符串指定位置的字符.参数index表示指定位置startsWith()和endsWith() 检查一个字符串是否以指定字符串开头或者结尾 public void testStartWithAndEndWith() { String str = " Thinking in Java"; System. out. println(str.endsWith("Java")); // true System. out. println(str.startsWith("T"));// true System. out.println(str.startsWith(" thinking”)); // false toUpperCase() ----------转大写toLowerCase()-----------转小写isEmpty();--------------检查空字符串length()----------------字符串长度

    StringBuilder

    字符串构建器,可改变字符串,非线程安全,并发处理性能较快append()追加字符串内容insert()插入字符串delete()删除字符串replace()------------替换字符串revers()-------------字符串反转

    StringBuffer

    具有线程安全性,同步处理,性能较慢
    Processed: 0.012, SQL: 12