StringBuffer in Java with Examples - Java 147

StringBuffer in Java with Examples – Java 147

StringBuffer in Java with Examples

用於建立可修改字串,是一個同步的操作,因為 String 類別建立了一個不可變的字串,所以使用 StringBuffer 類別來建立字串,用來取代 String 類別的替代方案,StringBuffer in Java 介紹常見的 appendCodePoint 、 codePoints 、 subSequence 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- string
       |                   +- stringbuffer
       |                       +- StringBufferWithExamplesTest.java  

單元測試

提供新增、反轉等操作 String 。

appendCodePoint

建立一個 StringBuffer ,增加 codePoint 。

	@Test
	public void appendCodePoint() {
		int expectedSize = 17;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.appendCodePoint(32);
		builder.appendCodePoint(65);
		System.out.println(builder);
		assertEquals(expectedSize, builder.length());
	}
Spring boot 168 A

codePoints

建立一個 StringBuffer ,增加字串,轉成 codePoint 。

	@Test
	public void codePoints() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		IntStream stream = builder.codePoints();
		List<Integer> list = stream.boxed().collect(Collectors.toList());
		System.out.println(list);
	}
[83, 112, 114, 105, 110, 103, 32, 98, 111, 111, 116, 32, 49, 54, 56]

codePointAt

StringBuffer Java 建立一個 StringBuffer ,增加字串後,取得指定索引的 codePoint 。

	@Test
	public void codePointAt() {
		int expected = 103;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointAt(5);
		System.out.println(result);
		assertEquals(expected, result);
	}
103

codePointBefore

StringBuffer Java 建立一個 StringBuffer ,增加字串後,取得指定索引前的 codePoint 。

	@Test
	public void codePointBefore() {
		int expected = 110;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointBefore(5);
		System.out.println(result);
		assertEquals(expected, result);
	}
110

codePointCount

Java StringBuffer 建立一個 StringBuffer ,增加字串,取得 codePoint 數量。

	@Test
	public void codePointCount() {
		int expected = 5;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointCount(0, 5);
		System.out.println(result);
		assertEquals(expected, result);
	}
5

offsetByCodePoints

Java StringBuffer 建立一個 StringBuffer ,建立一個 StringBuffer ,增加字串後,取得指定索引偏移量的 codePoint 。

	@Test
	public void offsetByCodePoints() {
		int expected = 5;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.offsetByCodePoints(1, 4);
		System.out.println(result);
		assertEquals(expected, result);
	}
5

subSequence

Java StringBuffer 建立一個 StringBuffer ,增加字串後,取得指定索引區間的字串。

	@Test
	public void subSequence() {
		CharSequence expected = "Spring";
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		CharSequence result = builder.subSequence(0, 6);
		System.out.println(result);
		assertEquals(expected, result);
	}
Spring

reverse

Java StringBuffer 建立一個 StringBuffer ,增加字串後,取得反轉字串。

	@Test
	public void reverse() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168, ");
		builder.append("JUnit 151");
		StringBuffer result = builder.reverse();
		System.out.println(result);
	}
151 tinUJ ,861 toob gnirpS

StringBufferWithExamplesTest.java

StringBuffer in Java 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.string.stringbuffer;

import static org.junit.Assert.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.Test;

public class StringBufferWithExamplesTest {

	@Test
	public void appendCodePoint() {
		int expectedSize = 17;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.appendCodePoint(32);
		builder.appendCodePoint(65);
		System.out.println(builder);
		assertEquals(expectedSize, builder.length());
	}

	@Test
	public void codePoints() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		IntStream stream = builder.codePoints();
		List<Integer> list = stream.boxed().collect(Collectors.toList());
		System.out.println(list);
	}

	@Test
	public void codePointAt() {
		int expected = 103;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointAt(5);
		System.out.println(result);
		assertEquals(expected, result);
	}

	@Test
	public void codePointBefore() {
		int expected = 110;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointBefore(5);
		System.out.println(result);
		assertEquals(expected, result);
	}

	@Test
	public void codePointCount() {
		int expected = 5;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.codePointCount(0, 5);
		System.out.println(result);
		assertEquals(expected, result);
	}

	@Test
	public void offsetByCodePoints() {
		int expected = 5;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.offsetByCodePoints(1, 4);
		System.out.println(result);
		assertEquals(expected, result);
	}

	@Test
	public void subSequence() {
		CharSequence expected = "Spring";
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		CharSequence result = builder.subSequence(0, 6);
		System.out.println(result);
		assertEquals(expected, result);
	}

	@Test
	public void reverse() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168, ");
		builder.append("JUnit 151");
		StringBuffer result = builder.reverse();
		System.out.println(result);
	}

}

心得分享

Java StringBuffer Example 提供了幾種 StringBuffer 常見方法的操作範例,在應用上相當廣泛,熟悉 Java StringBuffer 這些方法的操作,像是: appendCodePoint 、 codePoints 、 subSequence 等方法。

發佈留言