Java--基础字符串API(小部分)

JavaAPI十分强大,而且非常多,所以我们是记不过来的,当我们需要使用的时候要参照JavaAPI文档,不用特别的记忆,但我们学过的一些API要知道它是拿来干什么的,这样我们在开发过程才知道使用它,具体的使用方法我们可以到时候可以参照文档
文档链接:https://docs.oracle.com/javase/8/docs/api/

char charAt(int index)

/**
 * char charAt(int index)
 * 从字符串指定位置获取字符
 * @author 
 *
 */
	public static void main(String[] args) {
		//			  01234567
		String str = "thinging";
		char str1 = str.charAt(5);
		System.out.println("获取的字符为:" + str1);//获取的字符为:i
		}

int indexOf(String str)

/**
 * int indexOf(String str)
 * 返回给定字符串在当前字符串中的位置,若当前字符串中
 * 不包含该内容有则返回-1
 * @author 
 *
 */
public static void main(String[] args) {
		String s1 = "thinking in java";
		int index = s1.indexOf("in");
		index = s1.indexOf("In");
		System.out.println(index);//-1
		//可以从指定位置开始找
		index = s1.indexOf("in",2);
		//查找最后一次出现的位置
		index = s1.lastIndexOf("in");
		System.out.println(index);//9
	}

boolean startsWith(String prefix)
boolean endsWith(String suffix)

/**
 * boolean startsWith(String prefix)
 * boolean endsWith(String suffix)
 * 判断给定字符串是否是该字符串的开头或者结尾,是则返回
 * true,不是返回false
 * @author 
 *
 */
	public static void main(String[] args) {
		String no = "Thinking";
		boolean starts = no.startsWith("Thi");
		boolean ends = no.endsWith("g");
		System.out.println(starts);//true
		System.out.println(ends);//true
	}

String substring(int beginIndex)

/**
 * String substring(int start, int end)
 * 截取指定范围内的字符串。两个参数为开始到结束的下标。
 * 注:java api有一个特点,通常用两个数字表示范围都是“
 * 含头不含尾”的。
 * @author 
 *
 */
public class SubstringDemo {

	public static void main(String[] args) {
		//             012345678901234
		String host = "www.charing.com";
		
		String sub = host.substring(4, 11);
		System.out.println(sub);//charing
		//一个参数,截取到末尾	
		sub = host.substring(4);
		System.out.println(sub);//charing.com
	}
}

String toLowerCase()
String toUpperCase()

/**
 * String toUpperCase()
 * String toLowerCase()
 *返回一个新字符串。这个字符串将原始字符串中的大写字母
 *改为小写,或者将原始字符串中的所有小写改为大写字母
 * @author 
 *
 */
public class toUpperCase {
	public static void main(String[] args) {
		String str = "我爱Java";
		String str1 = str.toUpperCase();
		System.out.println(str1);//我爱JAVA
		String str2 = str.toLowerCase();
		System.out.println(str2);//我爱java

	}
}

String trim()

/**
 * String trim()
 * 返回一个新的字符串,这个字符串将删除原始字符串头部
 * 和尾部的空格
 * @author 
 *
 */
public class trim {
	public static void main(String[] args) {
		String str = "Hello    ";
		String dstr = str.trim();
		System.out.println(str.length());//9
		System.out.println(dstr);//Hello
	}
}

String valueOf(int index)

/**
 * String提供一组重载静态的方法:valueOf
 * 可以将给定的内容转换为字符串
 * @author 
 *
 */
public class ValueOfDemo {

	public static void main(String[] args) {
		int d = 1;
		String str = String.valueOf(d);
		double q = 1.1;
		String str1 = String.valueOf(q);
		System.out.println(str + 2);//12
		System.out.println(str1);//1.1(字符)
		
		str  = d +"3"; /*这种方法效率低,一次两次可以,
		多次还是使用上面的方法*/
	}
}

Object类的toString,equals

public class TestPoint {

	public static void main(String[] args) {
  		Point p = new Point(1,2);
		
		String str = p.toString();
		/**
		 * 通常我们定义的类需要用toString方法时,
		 * 就应该重写改方法,Object提供的是句柄,
		 * 没有实际意义
		 */
		System.out.println(str);
		Point p2 = new Point(1, 2);//(1,2)
		System.out.println(p==p2);//false
		/**
		 * 如果我们用定义的类equals方法,那么我们需要重写
		 * 这个方法,Object提供的方法内部本身就是用“=="进
		 * 行比较的,没有实际意义, Java	API提供的类,
		 * toString,equals都进行了妥善的重写。
		 */
		System.out.println(p.equals(p2));//true
		}
	}

子类

public class Point extends TestPoint {
	private int x;
	private int y;
	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}

	public String toString() {
		return "(" + 1 + "," + 2 + ")"; 
	}
	public boolean equals(Object obj) {
		if(obj==null) {
			return false;
		}
		if(this==obj) {
			return true;
		}

		if(obj instanceof Point) {
			Point p = (Point)obj;
			return this.x==p.x&&this.y==p.y;
		}
		return false;
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值