From 043722e1da2079d1738823e1266f10b1b54838c9 Mon Sep 17 00:00:00 2001 From: asukavuuyn <1346007099@qq.com> Date: Fri, 10 Oct 2025 00:12:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:charAt=E8=B6=8A=E7=95=8C=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/text/StrBuilder.java | 2 +- .../test/java/cn/hutool/core/text/StrBuilderTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java index 305da2923..87e299f90 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java @@ -486,7 +486,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { if(index < 0){ index = this.position + index; } - if ((index < 0) || (index > this.position)) { + if ((index < 0) || (index >= this.position)) { throw new StringIndexOutOfBoundsException(index); } return this.value[index]; diff --git a/hutool-core/src/test/java/cn/hutool/core/text/StrBuilderTest.java b/hutool-core/src/test/java/cn/hutool/core/text/StrBuilderTest.java index 474a71143..52f2d9222 100644 --- a/hutool-core/src/test/java/cn/hutool/core/text/StrBuilderTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/text/StrBuilderTest.java @@ -131,4 +131,13 @@ public class StrBuilderTest { helloWorld.insert(6, "Beautiful "); Assertions.assertEquals("Hello Beautiful World", helloWorld.toString()); } + + @Test + void charAtTest() { + final StrBuilder helloWorld = StrBuilder.create("Hello World"); + Assertions.assertEquals(helloWorld.charAt(-1),'d'); + Assertions.assertEquals(helloWorld.charAt(0),'H'); + Assertions.assertEquals(helloWorld.charAt(10),'d'); + Assertions.assertThrows(StringIndexOutOfBoundsException.class, () -> helloWorld.charAt(11));; + } }