首页 > Java > java教程 > 正文

Java 中的 substring() 函数

王林
发布: 2024-08-30 15:35:52
原创
817 人浏览过

Java 是最广为人知和使用的编程语言之一,提供了广泛的函数和方法。任何编程语言的主要方面之一是可用数据类型的列表。 Java 将数据类型分为原始数据类型和非原始数据类型,其中 String 是非原始数据类型,因为它指的是对象。该 String 对象具有多种方法来对字符串执行各种操作。其中一种方法是 java 中的 substring() 函数。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

substring(): 字符串的一部分是子字符串。 substring() 方法返回指定字符串的部分。 substring 方法返回一个新字符串,该字符串是该字符串的子字符串。

现在我们已经了解了字符串和子字符串方法是什么,让我们了解一下子字符串方法的标准语法。

语法:

public String substring(int beginIndex)
登录后复制

这将返回一个全新的字符串,它是传递的字符串的子字符串。它采用的参数是 beginIndex,它指定从何处选取 substring() 的索引点。从指定索引的字符开始到字符串末尾。

异常: 如果传递负索引或任何大于字符串原始长度的索引,则会抛出“IndexOutOfBoundsException”。

substring() 函数在 Java 中如何工作?

当调用java中的substring()函数时,它会根据传递的beginIndex参数创建一个新字符串,该字符串是原始字符串的一部分并返回它。当 beginIndex 被传递时,它在字符串中搜索指定的索引值,然后选择指定索引处存在的字符。在一个版本中,传递 beginIndex,而在 substring 方法的另一版本中,提供 beginIndex 和 endIndex。重要的是要理解这样一个事实:每次调用 substring 方法时,它都会创建一个新字符串,因为字符串数据类型是不可变的。

实现 substring() 函数的示例

让我们看看下面给出的在 java 中实现 substring() 函数的示例:

示例#1

代码:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring()");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring is : ");
System.out.println(sampleStr.substring(8));
}
}
登录后复制

代码说明:我们的第一个示例程序有一个简单的演示子字符串方法,带有起始索引参数。我们创建了我们的类,然后添加了我们的主类。声明我们的字符串数据类型及其变量名称和值。首先,我们将按原样打印原始字符串。然后我们将使用 substring 方法的效果打印新字符串。我们已将“beginIndex”指定为从第 8th 位置开始,这意味着我们的新子字符串将从第 8th 索引开始,从第 8th 中选取字符🎜> 字符串到末尾的位置。执行上述代码后,子字符串应为“Java substring()”。请参阅下面所附的输出屏幕截图。

输出:

Java 中的 substring() 函数

如您所见,我们首先按原样打印原始字符串,然后打印子字符串。

示例#2

我们现在将看到另一个示例,与第一个示例类似,但添加了参数“endIndex”。

代码:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring() and something extra.");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(8, 24));
}
}
登录后复制

代码说明:与之前的代码类似,我们有我们的类和主类。然后我们声明了我们的字符串,其中包含变量名和值,与之前相比,它相当长。接下来,我们打印了原始字符串,然后打印了新字符串。通过我们的 substring 方法,我们传递了两个参数,beginIndex 和 endIndex,即 8 和 24。这些索引定义了子字符串必须从哪个点开始以及在哪里结束。在我们的示例中,子字符串将从第 8 个 索引开始,选取第 8 个 索引处的任何字符,直到第 24 个 索引。因此,我们的新子字符串将位于第 8th 和 24th 索引之间,在第 8th 之前和第 24th 索引之后。执行上述代码后,子字符串应为“Java substring()”。

输出:

Java 中的 substring() 函数

正如您在输出中看到的,第 8 个 之前和第 24 个 之后的字符位置不是我们新创建的子字符串的一部分。最广泛使用的子字符串方法之一是从开头和结尾清除不必要的字符。

示例 #3

代码:

public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is just another example of java substring.");
System.out.print("The original substring: " +sampleStr);
int slength = sampleStr.length();
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(2, slength-9));
}
}
登录后复制

Code Explanation: In our third example, we have our class and main class within. We have a slightly different string here, and we have used the length method. Similar to earlier examples, we have printed our original string and then declared a new integer with slength, and the value is the length of the string. Then in our final print statement, our beginIndex is 2, meaning the substring will begin from the 2nd index, and the endIndex is slength -9, meaning whatever the length of the string is, the substring will end on 9 minus the original length. In our example, slength -9 will result in a substring to end on “s”.

Output:

Java 中的 substring() 函数

One of the important aspects to remember is, the original string is very long and holds the 1Gb size of an array. So, even if the substring being very small, it will be of 1GB. Though this will obviously stop the original string from being garbage collected, it is a case of memory leak, meaning a large amount of memory is acquired even though it won’t be used.

Conclusion

The string is one of the important data types, and the substring method simply returns a part of the original string. When the substring method is called, it creates a new string with an offset and count. We have demonstrated multiple examples with various methods to learn about substring. Code with its respective screenshots are attached.

以上是Java 中的 substring() 函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!