Java中的API和字符串

Java中的API和字符串

1 API概述

1.1 什么是API

API(Application Programming Interface)意思为:应用程序接口

已经写好的程序或功能,程序需要时直接调用,无需再次编写

1.2 为什么需要API

Java设计者将很多常用的功能写好,形成API(或称库类)

Java程序需要这些功能时,直接调用API即可,方便软件开发,降低了开发难度

1.3 常用API结构

java.lang 基础类。如字符串,多线程等,使用频率非常高,不需要import,直接使用

java.util 常用工具类,如集合,随机数产生器,日历,时钟等

java.io 文件操作,输入、输出操作

java.net 网络操作

java.math 数学运算相关操作

java.security 安全相关操作

java.sql 数据库访问

java.text 处理文字,日期,数字,信息的格式

2 文档注释

2.1 什么是文档注释

以/**开始, */结束的注释是文档注释,方便阅读文档理解类中的内容

2.2 为什么需要文档注释

只有文档注释的内容才能通过Java系统自带的Javadoc命令生成文档,而普通注释的内容不能生成在文档中,只能在代码中看到。

2.3 怎么使用文档注释

文档注释可以在三个地方使用:类、方法、常量

类上使用是用于说明当前类的设计目的,功能等

package cn.tedu.vip.doc;
/**
 *  这个类用于演示文档注释所用
 *  文档注释一般作用于;类,方法,属性
 *  编写内容一般是功能或者是设计目的的描述
 *  
 * @author tedu    //名字
 * @since JDK1.8   //编写时从什么开始
 * @version 1.0    //版本 前大后小
 * @see java.lang.String //看当前java类中用到了其他哪些类
 */
public class ApiDocDemo {
	/**
	 * sayHello中的问候语;"你好!"  
	 */
	public static final String MSG="你好!";
	/**
	 * 返回对给定用户的问候语
	 * @param name 用户名
	 * @return  问候的字符串
	 */
	public String sayHello(String name) {
		return name+MSG;
	}
}
2.4 生成文档

可以使用javadoc命令作用在已经编写好文档注释的java程序,自动生成项目文档

3 String 字符串

3.1 什么是字符串

java中的String就是字符串类型

3.2 为什么需要字符串

字符就是文字,是人类沟通,记录信息不可或缺的媒体

java中的char只能保存一个字符,保存一段文字太麻烦

使用字符串可以一个对象保存多个文字

3.3 字符串概述

String类型被final修饰

String类特殊的赋值方法

String本质为char数组

String是不可变对象

字符串使用Unicode编码保存

3.4 字符串常量池
package cn.tedu.vip.string;
/**
 * 字符串是不可改变对象,内容一旦创建不可修改
 * 若改变,一定是创建(引用)新对象
 * @author Tedu
 *
 */
public class StringDemo {

	public static void main(String[] args) {
		/*
		 * java中推荐我们使用""引起来字符串对象来直接赋值的方法
		 * 也就是推荐字面量直接赋值给变量
		 * 这种赋值会在字符串常量池中创建
		 * 对象可能会被重用
		 */
		String s1="123abc";
		String s2="123abc";
		//s1和s2指向同一个对象
		System.out.println(s1==s2);//返回值为true
		String s3="123abc";
		System.out.println(s2==s3);//true
		s1=s1+"!";
		System.out.println(s1);
		//修改是s1 s2 不变
		System.out.println(s2);
		//s1和s2不在同对象
		System.out.println(s1==s2);//false
		
		
		//使用new关键字来 创建对象(不推荐使用这种方法)
		String s4=new String("123abc");//不使用字符串常量池
		System.out.println(s4);
		System.out.println(s2==s4);//false
		String s5=new String("123abc");
		System.out.println(s4==s5);//false
		/*
		 * 比较字符串时,如果需要比较字符串中的内容
		 * 推荐使用equals方法进行比较
		 * 字符串的比较很少使用“==”
		 */
		System.out.println(s2.equals(s4));//true
		System.out.println(s4.equals(s5));//true
		
		/*
		 * 编译器有个特性,凡是能在编译期确定的结果
		 * 一定会在编译期运行计算
		 * 将编译的结果直接保存在class文件
		 * 不会每次运行时在计算,以提高运行效率
		 */
		
		System.out.println("----------");
		String s6="123"+"abc";//创建了一个对象123abc  常见面试题
		//已经转换为s6="123abc"
		System.out.println(s6);
		System.out.println(s6==s2);//true
		
		String ss="123";
		String s7=ss+"abc";//并不是往常量池里赋值的
		System.out.println(s7);
		System.out.println(s7==s2);//false
		
		//单引号和双引号意义完全不同
		String s8='1'+23+"abc";//1是char类型
		System.out.println(s8);//72abc
		System.out.println(s2==s8);//false
	}
}

4 字符串常用API

4.1 获得字符串长度

int length()

获取当前字符串长度(字符个数)

package cn.tedu.vip.string;
/**
 * int length()
 * 获取当前字符串长度
 * @author Tedu
 */
public class LengthDemo{
    
    public static void main(String[] args){
        String str="你好!java!";
        System.out.println(str.length());
    }
}
4.2 获得某些字符在字符串中的位置

int indexOf(String str)

查找给定字符串在当前字符串中的位置,若当前字符串不包含给定内容则返回值为-1

package cn.tedu.vip.string;
/**
 * indexOf(String str)
 * 查找给定字符串在当前字符串中的位置
 * 若存在返回给定字符串所在的索引位置
 * 若不存在返回-1
 * @author Tedu
 *
 */
public class IndexDemo {
    
	public static void main(String[] args) {
		String str="thinking in java";
		//          0123456789012345
		int index=str.indexOf("in");//单引号也可以
		System.out.println(index);//2
		
		//从指定位置开始检索第一次出现的字符
		index=str.indexOf("in",4);
		System.out.println(index);//5
		//找最后一个(最后出现的字符位置)
		index=str.lastIndexOf("in");
		System.out.println(index);//9
	}
}
4.3 从字符串中截取指定的内容

String substring(int start, int end)

截取当前字符串中指定范围内的字符串,两个参数分别表示开始的下标和结束的下标,注意:含头不含尾

package cn.tedu.vip.string;
/**
 * String(int start,int end)
 * 截取当前字符串中的内容
 * 按照留前不留后的规则
 * 返回截取到的内容
 * @author Tedu
 *
 */
public class SubStringDemo {

	public static void main(String[] args) {
		String str="www.tedu.cn";
		//          01234567890
		//截取域名tedu(两个参数时留前不留后)
		String sub=str.substring(4,8);
		System.out.println(sub);//tedu
		
		//一个参数表示从该位置开始截取到末尾
		sub=str.substring(4);
		System.out.println(sub);//tedu.cn
	}
}
4.4 取出域名
package cn.tedu.vip.string;

public class StringTest {
	//定义几个域名
	//通过编写一个方法能够返回www之后的域名内容

	public static void main(String[] args) {
		String s1="www.baidu.com";//baidu
		//         0123456789012
		String s2="http://www.tedu.cn";//tedu
		String s3="http://doc.tarena.com.cn";//tarena
		//要取出的内容是第一个点到第二个点之间的内容
		s1=getHostName(s1);
		s2=getHostName(s2);
		s3=getHostName(s3);
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		
	}
	//定义一个方法取出一个字符串中第一个点和第二个点之间的内容
	public static String getHostName(String ur1) {
		int start=ur1.indexOf(".")+1;//+1修正截取起点
		int end=ur1.indexOf(".",start);//留前不留后,end不需要修正
		String sub=ur1.substring(start, end);
		return sub;
		//return ur1.substring(start,end);//简写
	}
}
4.5 去掉字符串左右空格

String trim()

去掉当前字符串两边的空白字符

package cn.tedu.vip.string;
/**
 * trim()
 * 方法功能是去掉当前字符串左右两边的空格
 * @author Tedu
 *
 */
public class TrimDemo {

	public static void main(String[] args) {
		String str=" a b c d ";
		System.out.println("|"+str+"|");
		str=str.trim();
		System.out.println("|"+str+"|");

	}
}
4.6 根据位置获取字符

char charAt(int index)

获取当前字符串中指定位置的字符

package cn.tedu.vip.string;
/**
 * charAt(int index)
 * 返回指定索引位置的字符
 * @author Tedu
 *
 */
public class CharAtDemo {

	public static void main(String[] args) {
		String str="thinking in java";
		//          0123456789012345
		char c=str.charAt(12);
		System.out.println(c);

		//判断回文
		Boolean b=true;
		String str1="上海自来水来自海上";
		//           0123  5 6 7 8  len;9
		for(int i=0; i<str1.length()/2; i++) {
			char s=str1.charAt(i);
			char e=str1.charAt(str1.length()-1-i);
			if(s!=e) {
				b=false;
				break;
			}
		}
		if(b) {
			System.out.println("是回文");
		}else {
			System.out.println("不是回文");
		}
	}
}
4.7 判断字符串首尾内容

boolean startsWith(String str)

boolean endsWith(String str)

判断当前字符串是否是以给定的字符串开始或结尾:

package cn.tedu.vip.string;
/**
 * boolean startsWith(String str)
 * boolean endsWith(String str)
 * @author Tedu
 *判断当前字符串时候以给定的字符串开始或结束
 */
public class StartWithDemo {

	public static void main(String[] args) {
		String str="thinking in java";
		boolean starts=str.startsWith("thing");
		System.out.println(starts);//false
		
		boolean ends=str.endsWith("java");
		System.out.println(ends);//true
	}
}
4.8 转换大小写

String toUpperCase()

String toLowerCase()

将当前字符串中的英文部分转换为全大写或全小写:

package cn.tedu.vip.string;
/**
 * String toUpperCase();//转大写
 * String toLowerCase();//转小写
 * 将当前字符串中的英文字母转换成全大写或全小写
 * @author Tedu
 *
 */
public class ToUpperCaseDemo {

	public static void main(String[] args) {
		String str="你好Tom";
		String upper=str.toUpperCase();
		String lower=str.toLowerCase();
		
		str=str.substring(1,3);
		
		System.out.println(upper);
		System.out.println(lower);
		System.out.println(str);
	}
}
4.9 基本数据类型转换成字符串

String提供了一组重载的静态方法,valueOf作用是将java中其他类型转换为字符串:

package cn.tedu.vip.string;
/**
 * String valueOf(...)String 类中的静态
 * 功能是将指定的基本数据类型,转换成String类型的对象
 * @author Tedu
 *
 */
public class ValueOfDemo {

	public static void main(String[] args) {
		int a=123;
		String s1=String.valueOf(a);
		System.out.println(s1);//123
		
		double dou=1263.123;
		String s2=String.valueOf(dou);
		System.out.println(s2);//1263.123
		
		String s3=a+"";
		System.out.println(s3);//123
	}
}

5 StringBuilder可变长度字符串

5.1 什么是可变长度字符串

StringBuilder类在java.lang包下,该类维护一个可变长度的字符串

它本身并不是字符串,而是提供维护字符串功能的API

5.2 为什么需要StringBuilder

因为String类型对象不可变,在频繁修改时性能低下,内存开销大

package cn.tedu.vip.string;
/**
 * 字符串的频繁修改带来的性能损耗
 * @author Tedu
 *
 */
public class StringBuilderDemo01 {

	public static void main(String[] args) {
		String str="a";
		for(int i=0; i<1000000; i++) {
			str=str+"a";
		}
		System.out.println("over");
	}
}

上述代码运行会非常慢,极端情况下会发生异常,原因就是内存溢出了。使用StringBuilder来解决问题:

package cn.tedu.vip.string;
/**
 * 使用StringBuilder对象完成对字符串的频繁修改
 * @author Tedu
 *
 */
public class StringBuilderDemo02{

	public static void main(String[] args) {
		StringBuilder builder=new StringBuilder("a");
		for(int i=0; i<1000000; i++) {
			builder.append("a");
		}
		System.out.println("over");
	}
}
5.3 StringBuilder操作字符串

java.lang.StringBuilder

该类内部维护一个可变的字符数组,修改都在这里进行。

package cn.tedu.vip.string;
/**
 * 使用StringBuilder对象完成对字符串的频繁修改
 * @author Tedu
 *
 */
public class StringBuilderDemo1{
    public static void main(String[] args){
        String str="好好学习java";
        
        String Buffer builder=new StringBuffer(str);
        /*
 		 * 好好学习java
         * 好好学习java,为了找个好工作!
         *append():追加内容
         */
        builder .append(",为了找个好工作!");
        
        str=builder.toString();
        System.out.println(str);
        
        /*
         * 好好学习java,为了找个好工作!
         * 好好学习java,就是为了改变世界!
         *replace():修改操作
         */	
        builder.replace(9,16,"就是为了改变世界!");
        System.out.println(builder);
        
        /*
         * 好好学习java,就是为了改变世界!
         * ,就是为了改变世界!
         *delete():删除操作
         */
        builder.delete(0,8);
        System.out.println(builder);
        
        /*
         * ,就是为了改变世界!
         *活着,就是为改变世界!
         */
        builder.insert(0,"活着");
            System.out.println(builder);

    }
}
posted @ 2021-03-07 23:39  指尖上的未来  阅读(158)  评论(0)    收藏  举报