使用String、StringBuilder和StringBuffer

String的一个静态成员变量CASE_INSENSITIVE_ORDER,说没有考虑到地区,所以可能会产生不让人满意的结果。我的疑惑是,地区性是什么?还有不让人满意的结果又是什么?
还有,这个变量要怎么用?
关于String.CASE_INSENSITIVE_ORDER的作用
这篇博客有提到,
自己也试一下,

charAt()
chars()涉及IntStream先放着
codePointAt()
codePointCount(),主要和length()有区别,在遇到需要一对代码单元表示的辅助字符的时候,length()返回2,codePointCount()返回1;
codePoints()涉及到IntStream先放着
compareTo()返回的是一个很大的数字,不是-1、0、1这样子的了。通过a compareTo A返回32可以看出返回的数字是ascii的相减结果
compareIgnoreCase()
concat()
contains()传入CharSequence还是String都可以,
contentEquals() 之所以要提供这个方法,估计是因为存在StringBuffer和StringBuilder这两个类,如果是使用equals的话,只对String和CharSequence对象并且相同内容返回true,对于虽然内容相同,但是对象是StringBuffer或者StringBuilder这两个类,返回false,只有contentEquals()不管对象,只管内容返回true
copyValueOf()
describeConstable()先放着
endsWith()
equals()
equalsIgnoreCase()
format():详细请看下文
JAVA字符串格式化-String.format()的使用
其实等于System.out.printf();
%e和%f不能传入int类型的变量
%%只是返回一个百分号
$要配合它前面的数字使用,

String.format("%1$d,%2$d",1,2);
输出就是1,2
如果写成
String.format("%2$d,%1$d",1,2);
输出就是2,1

所以这就是为什么它叫参数索引的原因
%tN,后面多出的都是0,有什么用处?

formatted()使用它报错了,不知道原因是什么,也没搜到办法
在这里插入图片描述
getBytes()
对于中文,gbk和utf-8的编码不同,英文字母是相同的
getChars()
hashCode()
indent()不知道为什么找不到方法
在这里插入图片描述
indexOf()
intern()
isBlank()
isEmpty()
join()
lastIndexOf()中传入的fromIndex指的是最后一个字符的位置,然后从fromIndex往前搜索
length()
lines()涉及到了Stream,先放着
matches()
offsetByCodePoints()这个方法,看不懂,先放着
regionMatches()
replace()
CharSequence 对象不能转成String对象,但是String对象可以转成CharSequence对象。
replaceAll()使用的第一个参数是正则表达式,replace的话,输入\w只会替换\w这个字符,而不是字母,replaceAll则会替换所有字母
replaceFirst()
resolveConstantDesc()先放着
split()
startsWith()
stripIndent()//找不到这个方法
stripLeading()
stripTrailing()
subSequence()
substring()
toCharArray()
toLowerCase()
toString() 这个方法我没法理解。。。String.toString()??
toUpperCase()
transform()也找不到,先放着
translateEscapes()又找不到符号,先放着
trim()和strip()的区别请看
Java: trim()方法和strip()方法之间的区别

import java.util.*;
import java.nio.charset.*;
public class Test
{
	public static void main(String[] args) throws Exception
	{
		//String
		//文档中的例子,可以把char数组直接用String构造器,变成一个String对象
		char[] data = {'a','b','c'};
		String str = new String(data);
		System.out.println(str);
		//String的类变量
		System.out.println(String.CASE_INSENSITIVE_ORDER);
		//结合这个变量和Arrays使用一下。
		String[] one = {"a","d","c"};
		String[] two = {"a","d","d"};
		Arrays.sort(one,String.CASE_INSENSITIVE_ORDER);
		System.out.println(Arrays.toString(one));
		System.out.println(Arrays.compare(one,two,String.CASE_INSENSITIVE_ORDER));

		//构造器使用
		String a = new String();//创建一个空字符串
		System.out.println(a);
		//想要测试byte[],就要先创造一个byte[]
		byte[] by = "你好啊,朋友!".getBytes();//把每个字符拆开,变成数字,对应ascii值
		System.out.println(Arrays.toString(by));
		//现在通过平台默认字符集解码这个byte
		String b = new String(by);
		System.out.println(b);//中英文,都可以正常解码

		//指定byte的起始点,和长度,来解码
		String c = new String(by,0,3);
		System.out.println(c);//中英文,长度没错的话都可以正常解码,如果只有单个字节的话,会显示?
		
		//用字符串指定字符集,的上面的两种方法
		String d = new String(by,"GBK");//windows是采用GBK的,所以by其实是GBK编码的,
		System.out.println(d);//中英文,都可以正常解码
		String e = new String(by,0,2,"gbk");
		System.out.println(e);//中英文,都可以正常解码
		
		//用Charset指定字符集,再用上面两种方法
		//先看看Charset支持什么字符集
		SortedMap<String,Charset> charsetNames = Charset.availableCharsets();
		for(String s:charsetNames.keySet())
		{
			System.out.println(s);
		}
		//charset通过forName()创建对象
		Charset charset = Charset.forName("utf-8");//获得charset对象
		String f = new String(by,charset);
		System.out.println(f);

		Charset charset2 = Charset.forName("gbk");
		String g = new String(by,0,4,charset2);
		System.out.println(g);


		//使用char[]创建String
		char[] five = {'a','t','f'};
		String h = new String(five);
		System.out.println(h);
		
		String i = new String(five,0,2);
		System.out.println(i);

		int[] codepoint = {97,98,99,100};
		String j = new String(codepoint,0,3);
		System.out.println(j);
		
		//创建一个j的副本
		String jcopy = new String(j);
		System.out.println(jcopy);

		System.out.println(j == jcopy);
	
		//用StringBuffer创建字符
		String k = new String(new StringBuffer("aaaa"));
		System.out.println(k);

		//用StringBuilder创建字符
		String l = new String(new StringBuilder("bbbb"));
		System.out.println(l);

		//使用String的方法
		String m = "你好啊,朋友!";
		char ch = m.charAt(5);
		System.out.println(ch);

		int code = m.codePointAt(1);
		System.out.println(code);
	
		code = m.codePointBefore(1);
		System.out.println(code);

		String n = "\uD835\uDD6B";
		System.out.println(n);
		code = n.codePointCount(0,2);
		System.out.println(code);
		System.out.println(n.length());

		String o = "a";
		String p = "A";
		System.out.println(o.compareTo(p));
		System.out.println(o.compareToIgnoreCase(p));

		String q = o.concat(p);//效果和 o+p 相同
		System.out.println(q);
		System.out.println(q==o);//并不是同一个对象,

		a = "我是皮卡丘";
		//CharSequence cs = "皮卡丘";
		String cs = "我是";
		System.out.println(a.contains(cs));

		//CharSequence cs2 = "我是皮卡丘";
		//StringBuffer cs2 = new StringBuffer("我是皮卡丘");
		//String cs2 = "我是皮卡丘";
		StringBuilder cs2 = new StringBuilder("我是皮卡丘");
		System.out.println(a.contentEquals(cs2));
	
		char[] six = {'a','b','c'};
		j = String.copyValueOf(six);
		j = String.copyValueOf(six, 0 ,1);
		//j = String.copyValueOf("aaa");//只能用char[]
		System.out.println(j);

		j = "huchi咕咕咕";
		System.out.println(j.endsWith("咕咕"));
		System.out.println(j.endsWith("忽视"));
		
		i = "我是";
		j = "我是";
		StringBuffer sbf = new StringBuffer("我是");
		StringBuilder sbl = new StringBuilder("我是");
		CharSequence cs3 = "我是";
		
		System.out.println(i.equals(j));
		System.out.println(i.equals(sbf));
		System.out.println(i.equals(sbl));
		System.out.println(i.equals(cs3));

		i = "a";
		j = "A";
		h = "a";
		System.out.println(i.equalsIgnoreCase(j));
		System.out.println(i.equalsIgnoreCase(h));

		System.out.println(String.format("Hi,%s!","小李"));
		System.out.println(String.format("你好,%s,%s,%s。","小王","joker","小李"));
		System.out.printf("你好,%s,%s,%s。%n","小王","joker","小李");
		String str2 = String.format("%c, %b,%d,%x,%o,%3.2f,%a,%e,%h,%d%%,%g,%tc %n",'a',true,10,10,10,10.0,10.0,10.0,"我",70,100.0,new Date());
		System.out.println(str2);

		str2 = String.format("格式参数$的使用:%2$s, %1$d",-99,"我是");
		System.out.println(str2);

		str2 = String.format("显示正负数的符号:%+d与%d %n",99,99);
		System.out.println(str2);
		str2 = String.format("显示正负数的符号:%+d与%d %n",-99,-99);
		System.out.println(str2);

		str2 = String.format("总长度为10位的,补充0的数字 :%010d",7);
		System.out.println(str2);

		str2 = String.format("总长度为10位的,补充空格的数字:% 10d",1);
		System.out.println(str2);

		str2 = String.format("总长度为10的钱,看看有多少个逗号:%,d",100000000);
		System.out.println(str2);

		str2 = String.format("一本书的价格:% 50.5f元",49.8);
		System.out.println(str2);
		
		Date date = new Date();
		str2 = String.format("%tc",date);
		System.out.println(str2);
		
		str2 = String.format("%tF",date);
		System.out.println(str2);

		str2 = String.format("%tD",date);
		System.out.println(str2);

		str2 = String.format("%tr",date);
		System.out.println(str2);

		str2 = String.format("%tT",date);
		System.out.println(str2);

		str2 = String.format("%tR",date);
		System.out.println(str2);

		str2 = String.format("%tb",date);
		System.out.println(str2);

		str2 = String.format(Locale.US,"%tb",date);
		System.out.println(str2);

		str2 = String.format(Locale.US,"%tB",date);
		System.out.println(str2);

		str2 = String.format("%tB",date);
		System.out.println(str2);

		str2 = String.format(Locale.US,"%ta",date);
		System.out.println(str2);

		str2 = String.format("%ta",date);
		System.out.println(str2);

		str2 = String.format(Locale.US,"%tA",date);
		System.out.println(str2);
	
		str2 = String.format("%tA",date);
		System.out.println(str2);

		str2 = String.format("%tC",date);
		System.out.println(str2);

		str2 = String.format("%ty",date);
		System.out.println(str2);

		str2 = String.format("%tj",date);
		System.out.println(str2);

		str2 = String.format("%tm",date);
		System.out.println(str2);

		str2 = String.format("%td",date);
		System.out.println(str2);

		str2 = String.format("%te",date);
		System.out.println(str2);

		str2 = String.format("%tH",date);
		System.out.println(str2);

		str2 = String.format("%tI",date);
		System.out.println(str2);

		str2 = String.format("%tk",date);
		System.out.println(str2);

		str2 = String.format("%tl",date);
		System.out.println(str2);

		str2 = String.format("%tM",date);
		System.out.println(str2);
		
		str2 = String.format("%tS",date);
		System.out.println(str2);

		str2 = String.format("%tL",date);
		System.out.println(str2);
	
		str2 = String.format("%tN",date);
		System.out.println(str2);

		str2 = String.format(Locale.US,"%tp",date);
		System.out.println(str2);

		str2 = String.format("%tz",date);
		System.out.println(str2);
	
		str2 = String.format("%tZ",date);
		System.out.println(str2);

		str2 = String.format("%ts",date);
		System.out.println(str2);

		str2 = String.format("%tQ",date);
		System.out.println(str2);

		/*
		String str3 = new String("%d");
		str2 = str3.formatted(10);
		System.out.println(str2);
		*/

		by = "abc".getBytes();
		System.out.println(Arrays.toString(by));

		by = "a李".getBytes("utf-8");
		System.out.println(Arrays.toString(by));

		by = "a李".getBytes(Charset.forName("gbk"));
		System.out.println(Arrays.toString(by));
	
		char[] ch2 = new char[5];
		"abcdefg".getChars(0,5,ch2,0);
		System.out.println(Arrays.toString(ch2));

		System.out.println("你好".hashCode());

		//String str3 = new String("你好\n");
		//System.out.println(str3.indent(1));

		str2 = "皮卡丘、\n小火龙";
		System.out.println(str2.indexOf('皮'));
		System.out.println(str2.indexOf('皮',1));
		System.out.println(str2.indexOf("火"));
		System.out.println(str2.indexOf("火",2));

		str2 = new String("1");
		String str3 = new String(str2);
		System.out.println(str2 == str3);
		System.out.println(str2.equals(str3));
		System.out.println(str2.intern() == str3.intern());
		System.out.println(str2.intern());

		str2 = new String();
		System.out.println(str2.isBlank());
		str2 = "";
		System.out.println(str2.isBlank());

		str2 = "";
		System.out.println(str2.isEmpty());
		str2 = new String();
		System.out.println(str2.isEmpty());
		
		str2 = String.join("]",new String[] {"1","2","3"});
		System.out.println(str2);

		str2 = "我sfsdasdf我";
		System.out.println(str2.lastIndexOf('f'));
		System.out.println(str2.lastIndexOf('f',4));
		System.out.println(str2.lastIndexOf("f"));
		System.out.println(str2.lastIndexOf("f",4));

		System.out.println(str2.length());

		str2 = "abc";
		System.out.println(str2.matches("abc"));
		System.out.println(str2.matches("\\w+"));
		str2 = ""+(char)0x10123;
		System.out.println(str2.offsetByCodePoints(0,1));
		
		str2 = "abababab";
		System.out.println(str2.regionMatches(false, 0,"Abc",0,3));
		System.out.println(str2.regionMatches( true,0,"Abc",0,3));

		System.out.println(str2.repeat(3));

		System.out.println(str2.replace("\\w","o"));

		CharSequence ch1 = "\\w";
		CharSequence ch3 = "uu";
		//String str5 = ch1;
		System.out.println(str2.replaceAll("\\w","uu"));

		System.out.println(str2.replaceFirst("\\w","d"));

		str2 = "a2b,2c35  666";
		System.out.println(Arrays.toString(str2.split(",")));
		System.out.println(Arrays.toString(str2.split("\\d")));

		System.out.println(Arrays.toString(str2.split("\\d",2)));
		System.out.println(Arrays.toString(str2.split("\\d",0)));
		System.out.println(Arrays.toString(str2.split("\\d",-1)));

		System.out.println(str2.startsWith("a2b"));
		System.out.println(str2.startsWith("2b",0));

		str2 = "       a           ";
		System.out.println(str2);
		System.out.println(str2.strip());
		//System.out.println(str2.stripIndent());
		System.out.println(str2.stripLeading());
		System.out.println(str2.stripTrailing());

		str2 = "你好啊,流浪者";
		CharSequence cs4 = str2.subSequence(2,5);
		System.out.println(cs4);

		System.out.println(str2.substring(3));
		System.out.println(str2.substring(3,6));
		
		char[] cha = str2.toCharArray();
		System.out.println(Arrays.toString(cha));

		str2 = "HELLO,WORLD!";
		System.out.println(str2.toLowerCase());
		System.out.println(str2.toLowerCase(Locale.US));
		System.out.println(str2.toString());
		str2 = "hello,world";
		System.out.println(str2.toUpperCase());
		System.out.println(str2.toUpperCase(Locale.US));

		System.out.println("\u3000a\u3000".trim());
		System.out.println("\u3000a\u3000".strip());

		System.out.println("\u0020a\u0020".trim());
		System.out.println("\u0020a\u0020".strip());

		System.out.println(String.valueOf(true));
		System.out.println(String.valueOf('a'));
		System.out.println(String.valueOf(new char[] {'a','b','c'}));
		System.out.println(String.valueOf(new char[] {'a','b','c'},0,1));
		System.out.println(String.valueOf(10.5));
		System.out.println(String.valueOf((float)10.5));
		System.out.println(String.valueOf(10));
		System.out.println(String.valueOf(1L));
		System.out.println(String.valueOf(new Object()));
	}
}


StringBuilder
构造器,创建的StringBuilder对象,如果有CharSequence或者String的话,是在默认capacity的基础上加上对象参数的length();
append()
appendCodePoint()
capacity()
charAt()
chars()涉及到流,先放着
codePointAt()
codePointBefore()
codePointCount()
codePoints()涉及到流,先放着
compareTo()
delete()
deleteCharAt()
ensureCapacity(),如果传入的参数小于当前StringBuilder的capacity,
getChars()
indexOf()
insert()
lastIndexOf()
length()
offsetByCodePoints() 这个方法也没弄清楚,不知道应该在哪里用
replace()
reverse()
setCharAt()
setLength()
subSequence()
substring()
toString()
trimTosize()

import java.util.*;
import java.nio.charset.*;
public class Test
{
	public static void main(String[] args) throws Exception
	{
		//StringBuilder
		//构造器
		StringBuilder sb = new StringBuilder();//默认16个字符
		sb = new StringBuilder(10);
		CharSequence cs = "cs";
		sb = new StringBuilder(cs);

		sb = new StringBuilder("我是撒旦法");
		sb.append(true);
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());

		//方法
		sb.append(false);
		sb.append('a');
		sb.append(new char[] {'a','a','b'});
		sb.append(new char[]{'d','d','d'},1,2);
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());

		sb.append(10.5);
		sb.append((float)10.8);
		sb.append(10);
		sb.append(10L);

		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());

		cs = "Aloha";
		sb.append(cs);
		
		sb.append(cs,0,2);
		sb.append(new Object());
		sb.append("你好");
		StringBuffer sbu = new StringBuffer("你好");
		sb.append(sbu);
		
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());

		sb.appendCodePoint('\u9999');
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		System.out.println(sb.capacity());

		System.out.println(sb.charAt(10));

		System.out.println(sb.codePointAt(10));
		System.out.println(sb.codePointBefore(10));
		System.out.println(sb.codePointCount(10,20));
		
		StringBuilder sb2 = new StringBuilder("20");
		System.out.println(sb.compareTo(sb2));
		
		System.out.println(sb);
		sb2 = sb.delete(2,30);
		System.out.println(sb);
		System.out.println(sb2);
		System.out.println(sb == sb2);

		System.out.println(sb.deleteCharAt(0));
		System.out.println(sb.capacity());
		sb.ensureCapacity(200);
		System.out.println(sb.capacity());

		System.out.println(sb);
		char[] ch = new char[10];
		sb.getChars(0,10,ch,0);
		System.out.println(Arrays.toString(ch));

		System.out.println(sb.indexOf("是"));
		System.out.println(sb.indexOf("是",2));

		System.out.println(sb.insert(0,true));
		System.out.println(sb.insert(0,'i'));
		System.out.println(sb.insert(0,new char[] {'a','b'}));
		System.out.println(sb.insert(0,new char[] {'a','b'},0,1));
		System.out.println(sb.insert(0,10.5));
		System.out.println(sb.insert(0,(float)10.5));
		System.out.println(sb.insert(0,10));
		System.out.println(sb.insert(0,10L));
		cs = "哦豁";
		System.out.println(sb.insert(0,cs));
		System.out.println(sb.insert(0,cs,0,1));		
		System.out.println(sb.insert(0,new Object()));
		System.out.println(sb.insert(0,"你好:"));
		System.out.println(sb.lastIndexOf("你"));
		System.out.println(sb.lastIndexOf("你",2));
		System.out.println(sb.length());
		System.out.println(sb.offsetByCodePoints(0,20));
		System.out.println(sb);
		System.out.println(sb.replace(0,20,"要改那边"));
		System.out.println(sb.reverse());
		System.out.println(sb);
		sb.setCharAt(0,'o');
		System.out.println(sb);

		System.out.println(sb.length());
		sb.setLength(80);
		System.out.println(sb.length());

		cs = sb.subSequence(0,5);
		System.out.println(cs);
		
		String str = sb.substring(10);
		System.out.println(str);
		str = sb.substring(10,20);
		System.out.println(str);

		System.out.println(sb.toString());

		System.out.println(sb.capacity());
		sb.trimToSize();
		System.out.println(sb.capacity());
		
		sb.ensureCapacity(500);
		System.out.println(sb.capacity());
		System.out.println(sb.length());
		sb.setLength(120);
		System.out.println(sb.capacity());
		System.out.println(sb.length());

		sb.setLength(600);
		System.out.println(sb.capacity());
		System.out.println(sb.length());
		
		
	}
}


StringBuffer
的方法和StringBuilder的方法完全一致。

现在测试一下二者的多线程的线程安全性

以下程序测试StringBuilder

import java.util.*;
import java.nio.charset.*;
public class Test
{
	public static void main(String[] args) throws Exception
	{
		//一个线程插入true,一个线程插入false,然后每次插入输出sb的length
		StringBuilder sb = new StringBuilder();
		MyBuilderThread mbt = new MyBuilderThread(sb,true);
		MyBuilderThread mbf = new MyBuilderThread(sb,false);

		mbt.start();
		mbf.start();
		
	}
}
class MyBuilderThread extends Thread
{
	//测试StringBuiler的线程安全性
	public StringBuilder sb;
	public boolean bool;
	public MyBuilderThread (StringBuilder sb, boolean bool)
	{
		this.sb = sb;
		this.bool = bool;
	}
	public void run()
	{
		//插入10次
		for(int i = 0; i < 10 ; i++)
		{
			sb.append(bool);
			System.out.println(sb.length());
		}
		

	}
}


我感觉都可以被同时访问,那么线程安全又意味着什么??

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流cpu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值