From 578856e176b395b03f2940e0c8fd07313523708a Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 20 Jan 2026 14:42:12 +0800 Subject: [PATCH] =?UTF-8?q?day08-=E5=B8=B8=E7=94=A8=E7=B1=BB=5FString?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95=5Flength=5Fconcat?= =?UTF-8?q?=5FcharAt=5FindexOf=5FsubString?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day08/src/com/inmind/string01/Demo04.java | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 day08/src/com/inmind/string01/Demo04.java diff --git a/day08/src/com/inmind/string01/Demo04.java b/day08/src/com/inmind/string01/Demo04.java new file mode 100644 index 0000000..7ff7df3 --- /dev/null +++ b/day08/src/com/inmind/string01/Demo04.java @@ -0,0 +1,73 @@ +package com.inmind.string01; +/* +常用类_String的获取方法_length_concat_charAt_indexOf_subString + +public int length () :返回此字符串的长度。 +public String concat (String str) :将指定的字符串连接到该字符串的末尾。 +public char charAt (int index) :返回指定索引处的 char值。 +public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 +public int indexOf(String str, int fromIndex) + +public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 +串结尾。 +public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。 + */ +public class Demo04 { + + public static void demo1(String[] args) { + //public int length () :返回此字符串的长度。 + String str = "123456"; + System.out.println(str.length());//便于我们遍历字符串 + } + + public static void demo2(String[] args) { + //public String concat (String str) :将指定的字符串连接到该字符串的末尾。 + String str1 = "hello"; + String str2 = "world"; + String str3 = str1.concat(str2); + System.out.println(str1);//hello + System.out.println(str2);//world + System.out.println(str3);//helloworld + } + + public static void demo3(String[] args) { + //public char charAt (int index) :返回指定索引处的 char值。 + String str = "helloworld"; + //从str中获取w的字符 + char c = str.charAt(5); + System.out.println(c);//w + //请遍历字符串,将每个字符打印输出 + for (int i = 0; i < str.length(); i++) { + System.out.println(str.charAt(i)); + } + } + + public static void demo4(String[] args) { + //public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 + //public int indexOf(String str, int fromIndex) + String str = "123java321java54321"; + //查找第一个j的索引 + int index = str.indexOf("j");//获取指定字符串的索引,如果找不到则返回-1 + System.out.println(index);//3 + //获取第一个java的索引 + index = str.indexOf("java"); + System.out.println(index);//3 + System.out.println("-----------------------"); + //获取第二个java的索引 + index = str.indexOf("java", 3+1 ); + System.out.println(index);//10 + } + + + public static void main(String[] args) { + //public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。 + //public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。 + String str = "helloworld123"; + //请给我截取一个world123的字符串 + String substring = str.substring(5); + System.out.println(substring);//world123 + //请给我截取一个world的字符串 + String substring1 = str.substring(5, 10); + System.out.println(substring1);//world + } +}