String abc = "{"text":{"content":"sss"},"attach".substring(19); substring.substring(0,substring.indexOf("\"")); 这段代码的abc最后是什么值
时间: 2023-09-03 20:12:06 浏览: 94
这段代码是错误的,因为在 "{"text":{"content":"sss"},"attach" 中缺少一个闭合的花括号,导致字符串格式不正确。如果修正代码为:
```
String abc = "{\"text\":{\"content\":\"sss\"},\"attach\":null}".substring(19);
abc = abc.substring(0, abc.indexOf("\""));
```
那么最后的 `abc` 值应该是 "sss",即字符串中 "text" 字段对应的 "content" 值。
相关问题
将下述语句进行优化,提炼成一个两个入参一个出参的方法 if (commaInt >= 0 && dunhaoInt >= 0 && fullStopInt >= 0) { if (commaInt > dunhaoInt) { regionNameStr = dunhaoInt > fullStopInt ? eventSum.substring(0, fullStopInt) : eventSum.substring(0,dunhaoInt); } else { regionNameStr = commaInt > fullStopInt ? eventSum.substring(0, fullStopInt) : eventSum.substring(0,commaInt); } } else if (commaInt >= 0 && dunhaoInt >= 0 && fullStopInt < 0) { regionNameStr = commaInt > dunhaoInt ? eventSum.substring(0,dunhaoInt) : eventSum.substring(0, commaInt); } else if (commaInt >= 0 && dunhaoInt < 0 && fullStopInt >= 0) { regionNameStr = commaInt > fullStopInt ? eventSum.substring(0,fullStopInt) : eventSum.substring(0, commaInt); } else if (commaInt < 0 && dunhaoInt >= 0 && fullStopInt >= 0) { regionNameStr = dunhaoInt > fullStopInt ? eventSum.substring(0,fullStopInt) : eventSum.substring(0, dunhaoInt); } else if (commaInt >= 0 && dunhaoInt < 0 && fullStopInt < 0) { regionNameStr = eventSum.substring(0, commaInt); } else if (commaInt < 0 && dunhaoInt >= 0 && fullStopInt < 0) { regionNameStr = eventSum.substring(0, dunhaoInt); } else if (commaInt < 0 && dunhaoInt < 0 && fullStopInt >= 0) { regionNameStr = eventSum.substring(0, fullStopInt); }
可以将代码提炼成一个方法,该方法有两个入参eventSum和minIndex,一个出参regionNameStr。其中minIndex表示逗号、顿号和句号中最小的下标值。
以下是示例代码:
```
public static String getRegionNameStr(String eventSum, int minIndex) {
if (minIndex >= 0) {
if (minIndex == eventSum.indexOf(",")) {
int otherIndex = Math.max(Math.max(eventSum.indexOf(","), eventSum.indexOf("。")), -1);
regionNameStr = otherIndex >= 0 ? (otherIndex > minIndex ? eventSum.substring(0, minIndex) : eventSum.substring(0, otherIndex)) : eventSum.substring(0, minIndex);
} else if (minIndex == eventSum.indexOf(",")) {
int otherIndex = Math.max(Math.max(eventSum.indexOf(","), eventSum.indexOf("。")), -1);
regionNameStr = otherIndex >= 0 ? (otherIndex > minIndex ? eventSum.substring(0, minIndex) : eventSum.substring(0, otherIndex)) : eventSum.substring(0, minIndex);
} else if (minIndex == eventSum.indexOf("。")) {
int otherIndex = Math.max(Math.max(eventSum.indexOf(","), eventSum.indexOf(",")), -1);
regionNameStr = otherIndex >= 0 ? (otherIndex > minIndex ? eventSum.substring(0, minIndex) : eventSum.substring(0, otherIndex)) : eventSum.substring(0, minIndex);
}
} else {
regionNameStr = eventSum;
}
return regionNameStr;
}
```
这样,原来的代码可以改为调用该方法,并传入minIndex作为参数。例如:
```
int minIndex = Math.min(Math.min(commaInt, dunhaoInt), fullStopInt);
regionNameStr = getRegionNameStr(eventSum, minIndex);
```
String.substring
The String.substring() method is used to extract a portion of a string and returns a new string. It takes two parameters: the starting index and the ending index. The starting index is the position at which the extraction should begin (inclusive), and the ending index is the position at which the extraction should end (exclusive). If the ending index is not provided, the substring will include all characters from the starting index to the end of the string.
Syntax:
string.substring(startIndex, endIndex)
Example:
let str = "Hello World";
let substr1 = str.substring(0,5); // "Hello"
let substr2 = str.substring(6); // "World"
阅读全文