酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java 实现构造串、判空、求长度及求子串操作的示例程序

以下是一个简单的示例程序,实现了构造串、判断串是否为空、返回串的长度和求子串的操作。 ```java public class MyString { private char[] chars; public MyString(String str) { chars = str.toCharArray(); } public boolean isEmpty() { return chars.length == 0; } public int length() { return chars.length; } public MyString substring(int start, int end) { if (start < 0 || end > chars.length) { throw new IndexOutOfBoundsException(); } char[] subChars = new char[end - start]; System.arraycopy(chars, start, subChars, 0, end - start); return new MyString(new String(subChars)); } public static void main(String[] args) { MyString str = new MyString("Hello World"); System.out.println("Length: " + str.length()); System.out.println("Is empty? " + str.isEmpty()); System.out.println("Substring: " + str.substring(6, 11)); } } ``` 该程序通过将字符串转换为字符数组进行操作,并提供了构造串、判断串是否为空、返回串的长度和求子串的方法。在主函数中示例了如何使用这些方法。 ######[AI写代码神器 | 778点数解答 | 2024-05-27 18:35:13]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]