String、StringBuffer、StringBuilder、Math学习笔记

String

* 不可变的String类 final String 
 * +-->字符串连接符以及将其他对象转为字符串的特殊支持
 *  比较、查找、复制、提取、转换、检查 操作

StringBuffer、StringBuilder

 * StringBuffer-->可变的字符序列 线程安全的字符缓冲区 
 * 在字符串缓冲区进行同步 
 * 主要操作是-->append(将字符添加到缓冲区末尾)
 * 			insert(将字符添加到指定位置)
 * 
 * StringBuilder-->可变字符序列
 *  但不保证同步,需外部同步 
 *  属于StringBuffer的简易版本 
 *  主要操作与StringBuffer一致

Math

 * Math类用于执行基本的数学运算
 * Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数



String测试代码

package com.undergrowth.lang;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

/**
 * 不可变的String类 final String 
 * +-->字符串连接符以及将其他对象转为字符串的特殊支持
 *  比较、查找、复制、提取、转换、检查 操作
 * 
 * @author Administrator
 * 
 */
public class StringLearn {

	public StringLearn() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 获取系统所有字符集
	 */
	@Test
	public void testAvailCharset() {
		Map<String, Charset> charsetMap = Charset.availableCharsets();
		System.out.println("总共有字符集:" + charsetMap.size());
		System.out.println("默认字符集为:" + Charset.defaultCharset().displayName());
		System.out.println("遍历支持字符集如下:");
		for (Map.Entry<String, Charset> charsetEntry : charsetMap.entrySet()) {
			System.out.println(charsetEntry.getKey() + ":"
					+ charsetEntry.getValue().displayName() + "\t"
					+ charsetEntry.getValue().name());
		}

	}

	/**
	 * 显示系统支持的语言相关的政治、地理、文化区域 系统支持的语言环境
	 */
	@Test
	public void testAvailLocale() {
		Locale[] locales = Locale.getAvailableLocales();
		for (Locale locale : locales) {
			System.out.println(locale.getCountry() + "\t"
					+ locale.getDisplayCountry() + "\t"
					+ locale.getDisplayName() + "\t" + locale.getLanguage()
					+ "\t" + locale.getDisplayLanguage());
		}
	}

	// 测试字符串
	String testString = "123@987@,qwert:@op";
	// 测试比较字符串
	String otherTestString = "123@987@,QWERT:@op";
	// 测试字符数组
	char[] testCharArray = testString.toCharArray();

	/**
	 * 测试字符串的查找功能
	 */
	@Test
	public void testLook() {
		System.out.println("待测试字符串为:" + testString);
		// 获取最后一个字符
		System.out.println("testString.charAt(testString.length()-1)-->"
				+ testString.charAt(testString.length() - 1));
		System.out.println("testString.codePointAt(testString.length()-1)-->"
				+ testString.codePointAt(testString.length() - 1));
		System.out
				.println("testString.codePointBefore(testString.length()-1)-->"
						+ testString.codePointBefore(testString.length() - 1));
		System.out
				.println("testString.codePointCount(testString.length()-2, testString.length())-->"
						+ testString.codePointCount(testString.length() - 2,
								testString.length()));
		// 查找索引
		System.out.println(testString.indexOf("qw"));
		System.out.println(testString.indexOf("qw", 3));
		System.out.println(testString.lastIndexOf("@"));
		System.out.println(testString.lastIndexOf("@", 3));
		// 长度
		System.out.println(testString.length());
		System.out.println(testString.offsetByCodePoints(2, 5));
	}

	/**
	 * 测试字符串比较
	 */
	@Test
	public void testComparable() {
		System.out.println(otherTestString.compareTo(testString));
		System.out.println(otherTestString.compareToIgnoreCase(testString));
		System.out.println(otherTestString.equals(testString));
		System.out.println(otherTestString.equalsIgnoreCase(testString));
		// 正则表达式进行匹配
		testString = "12345@qwert";
		String regex = "[0-9]{1,}@[a-z]{1,}";
		System.out.println(testString.matches(regex));
		// 正则表达式匹配
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(testString);
		System.out.println(matcher.matches());
		testString = "123@987@,qwert:@op";
		// 区域匹配
		System.out.println(testString.regionMatches(0, otherTestString, 0,
				testString.length()));
		System.out.println(testString.regionMatches(0, otherTestString, 0, 7));
	}

	/**
	 * 测试字符串复制
	 */
	@Test
	public void testCopy() {
		System.out.println(otherTestString.concat(testString));
		System.out.println(String.copyValueOf(testCharArray));
		System.out.println(String.copyValueOf(testCharArray, 2, 5));
		// 复制到字符数组
		char[] dst = new char[1024];
		testString.getChars(0, testString.length(), dst, 5);
		System.out.println(dst);
		// 字符串的内部规范化表示
		System.out.println(testString.intern());
	}

	/**
	 * 字符串检查
	 */
	@Test
	public void testCheck() {
		System.out.println(otherTestString.contains(testString));
		System.out.println(otherTestString.contentEquals(testString));
		// 结束
		System.out.println(testString.endsWith("op"));
		System.out.println(testString.endsWith("oP"));
		// 是否为空
		System.out.println(testString.isEmpty());
		// 开头
		System.out.println(testString.startsWith("23"));
		System.out.println(testString.startsWith("123"));
		System.out.println(testString.startsWith("@", 3));
	}

	/**
	 * 测试字符串转换
	 * 
	 * @throws UnsupportedEncodingException
	 */
	@Test
	public void testTransform() throws UnsupportedEncodingException {
		// String的静态方法进行转换
		System.out.println(String.format(testString));
		System.out.println(String.format(testString + "\t %d %o %x", 100, 100,
				100));
		System.out.println(String.format(Locale.CANADA, "你好," + testString
				+ "\t %d %o %x", 100, 100, 100));
		// 默认采用GBK字符集获取字节码
		System.out.println(testString.getBytes());
		System.out.println(testString.getBytes(Charset.forName("UTF-8")));
		System.out.println(testString.getBytes("ISO-8859-1"));
		// 转换为小写、大写
		System.out.println(otherTestString.toLowerCase());
		System.out.println(testString.toUpperCase());
		// 去除空白
		System.out
				.println(String.valueOf("  " + otherTestString + "  ").trim());
		System.out.println(String.valueOf(12.367));
	}

	/**
	 * 字符串提取操作
	 */
	@Test
	public void testExtract() {
		// 替换
		System.out.println(testString.replace("@", "连接符"));
		System.out.println(testString.replaceFirst("@", "连接符"));
		// 分割
		String[] testArrayStrings = testString.split("@");
		for (String string : testArrayStrings) {
			System.out.println(string);
		}
		// 截取
		System.out.println(testString.substring(3));
		System.out.println(testString.substring(3, 6));
	}

}



StringBuffer、StringBuilder

package com.undergrowth.lang;

import org.junit.Test;

/**
 * StringBuffer-->可变的字符序列 线程安全的字符缓冲区 
 * 在字符串缓冲区进行同步 
 * 主要操作是-->append(将字符添加到缓冲区末尾)
 * 			insert(将字符添加到指定位置)
 * 
 * StringBuilder-->可变字符序列
 *  但不保证同步,需外部同步 
 *  属于StringBuffer的简易版本 
 *  主要操作与StringBuffer一致
 * 
 * @author Administrator
 * 
 */
public class StringBufferBuilderLearn {

	String str = "测试StringBuffer字符串 ";
	StringBuffer sbBuffer = new StringBuffer();
	StringBuffer sbBuffer2 = new StringBuffer(30);
	StringBuffer sbBuffer3 = new StringBuffer(str);

	public StringBufferBuilderLearn() {
		// TODO Auto-generated constructor stub

	}

	/**
	 * 测试长度与容量
	 */
	@Test
	public void testLength() {
		System.out.println(sbBuffer.capacity());
		System.out.println(sbBuffer.length());
		System.out.println(sbBuffer2.capacity());
		System.out.println(sbBuffer2.length());
		System.out.println(sbBuffer3.capacity());
		System.out.println(sbBuffer3.length());
		//
		sbBuffer2.ensureCapacity(50);
		System.out.println(sbBuffer2.capacity());
		System.out.println(sbBuffer2.length());
	}

	/**
	 * 测试Append-->附加到缓冲区末尾
	 */
	@Test
	public void testAppend() {
		System.out.println(sbBuffer.append(Boolean.TRUE).append('a')
				.append(sbBuffer3));
	}

	/**
	 * 测试insert-->将字符添加到指定位置
	 */
	@Test
	public void testInsert() {
		System.out.println(sbBuffer3.insert(3, "插入字符"));
	}

	/**
	 * 查找方法与String的也很类似
	 */
	@Test
	public void testLook() {
		System.out.println(sbBuffer3);
		System.out.println(sbBuffer3.charAt(5));
		System.out.println(sbBuffer3.codePointAt(5));
		System.out.println(sbBuffer3.codePointBefore(5));
		// 获取字符数组
		System.out.println(sbBuffer);
		char[] dst = new char[1024];
		sbBuffer3.getChars(0, sbBuffer3.length(), dst, 0);
		System.out.println(sbBuffer.append(dst));
		// 索引
		System.out.println(sbBuffer3.indexOf("字符串"));
		//
		System.out.println(sbBuffer3);
		System.out.println(sbBuffer3.lastIndexOf("你好"));
	}

	/**
	 * 测试删除字符操作
	 */
	@Test
	public void testDelete() {
		System.out.println(sbBuffer3);
		System.out.println(sbBuffer3.deleteCharAt(5));
		System.out.println(sbBuffer3.delete(1, 5));
	}

	/**
	 * 测试其他操作
	 */
	@Test
	public void testOther() {
		System.out.println(sbBuffer3.toString());
		System.out.println(sbBuffer3.reverse());
		System.out.println(sbBuffer3);
		System.out.println(sbBuffer3.replace(0, 3, "替换字符串后的字符序列"));
		System.out.println(sbBuffer3.substring(0, 10));
		System.out.println(sbBuffer3);
		System.out.println(sbBuffer3.capacity());
		System.out.println(sbBuffer3.length());
		sbBuffer3.trimToSize();
		System.out.println(sbBuffer3.capacity());
		System.out.println(sbBuffer3.length());
	}
}

Math

package com.undergrowth.lang;

import org.junit.Test;

/**
 * Math类用于执行基本的数学运算
 * Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数
 * @author Administrator
 *
 */
public class MathLearn {

	public MathLearn() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 测试Math的常量
	 */
	@Test
	public void testMathConstant(){
		System.out.println(Math.E);
		System.out.println(Math.PI);
	}
	
	@Test
	public void testMathMethod(){
		//绝对值 Math.abs(-19)
		System.out.println("绝对值 Math.abs(-19)");
		System.out.println(Math.abs(-19));
		System.out.println("绝对值 Math.abs(19)");
		System.out.println(Math.abs(19));
		//向上取整 Math.ceil(-19.1)
		System.out.println("向上取整 Math.ceil(-19.1)");
		System.out.println(Math.ceil(-19.1));
		System.out.println("向上取整 Math.ceil(-19.8)");
		System.out.println(Math.ceil(-19.8));
		//立方根 
		System.out.println("立方根  Math.cbrt(9) x*x*x=9");
		System.out.println(Math.cbrt(9));
		//e的多少次幂
		System.out.println("e的多少次幂 Math.exp(2)");
		System.out.println(Math.exp(2));
		//向下取整
		System.out.println("向下取整 Math.floor(-19.2)");
		System.out.println(Math.floor(-19.2));
		System.out.println("向下取整 Math.floor(-19.7)");
		System.out.println(Math.floor(-19.7));
		//自然对数 e
		System.out.println("自然对数 e Math.log(Math.E)");
		System.out.println(Math.log(Math.E));
		//10为底的指数
		System.out.println("10为底的指数 Math.log10(10)");
		System.out.println(Math.log10(10));
		//最大数 
		System.out.println("最大数 Math.max(10, 20)");
		System.out.println(Math.max(10, 20));
		//最小数 
		System.out.println("最大数 Math.min(10, 20)");
		System.out.println(Math.min(10, 20));
		//第一个参数和第二参数之间的数 相邻
		System.out.println(Math.nextAfter(20, 30));
		System.out.println(Math.nextUp(20));
		//幂
		System.out.println("幂 Math.pow(10, 10)");
		System.out.println(Math.pow(10, 10));
		//随机数
		System.out.println("随机数[0,1) Math.random()");
		System.out.println(Math.random());
		//四舍五入
		System.out.println(Math.round(10.2));
		System.out.println(Math.round(10.8));
		System.out.println(Math.round(-10.2));
		System.out.println(Math.round(-10.8));
		//rint 最接近的整数
		System.out.println(Math.rint(10.2));
		System.out.println(Math.rint(10.6));
		System.out.println(Math.rint(-10.2));
		System.out.println(Math.rint(-10.6));
		// f × 2scaleFactor
		System.out.println(" f × 2scaleFactor Math.scalb(5, 2)");
		System.out.println(Math.scalb(5, 2));
		//参数的符号
		System.out.println(Math.signum(98));
		System.out.println(Math.signum(-98));
		//sqrt
		System.out.println(Math.sqrt(4));
		}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值