java - 编写一个测试类,测试java.lang.String类中的以下方法:length ,charAt ,substring ,indexOf
问题描述
我不知道如何编写所有这些代码,然后通过 junit 测试对其进行测试。显然,它们需要相当多的方法等。但我不知道从哪里开始,该做什么。任何帮助都将受到极大的欢迎。
解决方案
尝试让这段代码在 Java 环境中运行并编辑一些值以了解它在做什么。
public class Main {
public static void main(String args[]) {
// Create a String to test with
String testString = "some test text";
// Prints out the number of characters in the String
System.out.println(testString.length());
// Prints out the character at the index of '2'
System.out.println(testString.charAt(2));
// Prints out a new String starting at index '5' and ending at index '9'
System.out.println(testString.substring(5, 9));
// Prints out the index number that the word 'text' starts at
System.out.println(testString.indexOf("text"));
}
}
推荐阅读
- php - 需要在 PHP 中发送一个数组
- php - 如何仅在某些帖子类型上使用 apply_filters 或 add_filter?
- mysql - SQL 中的表可以有多个列作为仅引用另一个表的一个主键的外键吗?
- angular - 如何在柏树中定位自定义组件的属性?
- ibm-midrange - 使用 idb2 .NET 提供程序执行 PGM 命令
- c# - 为什么我的对象引用未设置为对象错误的实例?
- sas - 如何阻止 SAS 数据步骤打开命令提示符
- macros - CALL SYMPUT 在 %EVAL 函数中找到一个字符操作数
- jquery - 是否可以从js调用netlify函数
- include - 假设这些头文件在同一个文件中,头文件 A 中的每个代码是否都包含在头文件 B 中