mystring.substring(9,13)
时间: 2023-06-23 08:02:45 浏览: 91
这段代码表示截取mystring这个字符串中从第9个字符开始到第13个字符(不包括第13个字符)的子串。换句话说,就是从mystring这个字符串中取出第9个、第10个、第11个和第12个字符,组成一个新的字符串返回。需要注意的是,substring方法的第一个参数代表截取子串的起始位置,第二个参数代表截取子串的终止位置(不包括该位置对应的字符),且两个参数都必须是有效的字符串下标。如果第二个参数省略,则表示截取到字符串末尾。
相关问题
在Java库中提供了String类,给出你自己对下面方法的实现(将新类 命名为MyString2):publicMyString2(String s); public int compare(String s); public MyString2 substring(int begin); public MyString2 toUpperCaseO; public char[]toChars(); public static MyString2 valueOf(boolean b);
好的,我了解了您的要求。针对您的问题,以下是MyString2类的实现:
```
public class MyString2 {
private String str;
public MyString2(String s) {
this.str = s;
}
public int compare(String s) {
return this.str.compareTo(s);
}
public MyString2 substring(int begin) {
return new MyString2(this.str.substring(begin));
}
public MyString2 toUpperCase() {
return new MyString2(this.str.toUpperCase());
}
public char[] toChars() {
return this.str.toCharArray();
}
public static MyString2 valueOf(boolean b) {
return new MyString2(String.valueOf(b));
}
}
```
其中,compare()方法用于比较两个字符串的大小,其返回值为0表示两个字符串相等,返回值小于0表示当前字符串小于参数字符串,返回值大于0表示当前字符串大于参数字符串。substring()方法用于返回当前字符串的子串,从指定的起始位置开始到字符串末尾。toUpperCase()方法用于将当前字符串中的字母全部转换为大写形式。toChars()方法用于将当前字符串转换为字符数组。valueOf()方法用于返回一个布尔值的字符串表示形式。
实现 String类)在Java库中提供了String类,给出你自己对下面方法的实现(将新类 命名为MyString2): public MyString2(String s);public int compare(String s); public MyString2 substring(int begin);public MyString2 toUpperCaseO;public char[]toChars(); public static MyString2 valueOf(boolean b);并能够运行
以下是MyString2类的实现:
```java
public class MyString2 {
private char[] chars;
public MyString2(String s) {
this.chars = s.toCharArray();
}
public int compare(String s) {
char[] otherChars = s.toCharArray();
int len = Math.min(chars.length, otherChars.length);
for (int i = 0; i < len; i++) {
if (chars[i] != otherChars[i]) {
return chars[i] - otherChars[i];
}
}
return chars.length - otherChars.length;
}
public MyString2 substring(int begin) {
if (begin < 0 || begin >= chars.length) {
throw new IndexOutOfBoundsException();
}
char[] newChars = new char[chars.length - begin];
System.arraycopy(chars, begin, newChars, 0, newChars.length);
return new MyString2(new String(newChars));
}
public MyString2 toUpperCase() {
char[] newChars = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= 'a' && chars[i] <= 'z') {
newChars[i] = (char) (chars[i] - 'a' + 'A');
} else {
newChars[i] = chars[i];
}
}
return new MyString2(new String(newChars));
}
public char[] toChars() {
char[] newChars = new char[chars.length];
System.arraycopy(chars, 0, newChars, 0, chars.length);
return newChars;
}
public static MyString2 valueOf(boolean b) {
return b ? new MyString2("true") : new MyString2("false");
}
}
```
以下是测试代码:
```java
public class Main {
public static void main(String[] args) {
MyString2 s = new MyString2("hello");
System.out.println(s.compare("world")); // -15
System.out.println(s.compare("help")); // 7
System.out.println(s.substring(1).toChars()); // "ello"
System.out.println(s.toUpperCase().toChars()); // "HELLO"
System.out.println(MyString2.valueOf(true).toChars()); // "true"
}
}
```
输出结果:
```
-15
7
ello
HELLO
true
```