int startIndex = line.IndexOf("Start Frequency:"); int stopIndex = line.IndexOf("Stop Frequency:"); int stepIndex = line.IndexOf("Step Frequency:");有没有更简便的写法
时间: 2024-02-19 12:57:46 浏览: 73
可以使用C# 8.0中的Range和Index来简化代码。示例代码如下:
```
int startIndex = line.IndexOf("Start Frequency:");
int stopIndex = line.IndexOf("Stop Frequency:");
int stepIndex = line.IndexOf("Step Frequency:");
startIndex = startIndex != -1 ? startIndex + "Start Frequency:".Length : 0;
stopIndex = stopIndex != -1 ? stopIndex + "Stop Frequency:".Length : 0;
stepIndex = stepIndex != -1 ? stepIndex + "Step Frequency:".Length : 0;
var range = startIndex..(stopIndex > 0 ? stopIndex : stepIndex);
string frequency = line[range].Trim();
```
使用Range和Index可以将startIndex、stopIndex和stepIndex的计算和判断简化为一行代码,并且可以使用range对象来截取字符串,避免了多次调用Substring方法的麻烦。
相关问题
int startIndex = line.IndexOf("Start Frequency:"); if (startIndex >= 0) { startIndex += "Start Frequency:".Length ; // 得到起始位置 int endIndex = line.IndexOf(",", startIndex); if (endIndex >= 0) { string startfrequency = line.Substring(startIndex, endIndex - startIndex); startFrequency.Text = startfrequency.Trim(); } }更简洁的写法
可以使用C# 8.0中的Range和Index来简化代码。示例代码如下:
```
int startIndex = line.IndexOf("Start Frequency:");
if (startIndex >= 0)
{
startIndex += "Start Frequency:".Length;
int endIndex = line.IndexOf(",", startIndex);
if (endIndex >= 0)
{
string startfrequency = line[startIndex..endIndex].Trim();
startFrequency.Text = startfrequency;
}
}
```
使用Range和Index可以将Substring方法的调用简化为一个索引运算符,从而简化代码。同时,Range和Index也可以用于其他字符串操作,如Split、Replace等。
再精简优化下这个段代码 int batchCount = 500; int dataSizePerThread = Math.max(1, awardIds.size() / batchCount); List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>(); for (int i = 0; i < dataSizePerThread; i++) { int startIndex = i * batchCount; int endIndex = Math.min((i + 1) * batchCount, awardIds.size()); List<Long> subList = awardIds.subList(startIndex, endIndex); CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> { List<TCaAwardInfo> result = new ArrayList<>(); result = awardApiService.queryActivationCode(subList); return result; }, taskExecutor); futures.add(future); } List<TCaAwardInfo> activationCodeList = futures.stream() .flatMap(future -> future.join().stream()) .collect(Collectors.toList());
可以将for循环中的部分代码提取出来,简化代码如下:
int batchCount = 500;
int dataSizePerThread = Math.max(1, awardIds.size() / batchCount);
List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>();
for (int i = 0; i < dataSizePerThread; i++) {
int startIndex = i * batchCount;
int endIndex = Math.min((i + 1) * batchCount, awardIds.size());
List<Long> subList = awardIds.subList(startIndex, endIndex);
CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> awardApiService.queryActivationCode(subList), taskExecutor);
futures.add(future);
}
List<TCaAwardInfo> activationCodeList = futures.stream()
.flatMap(CompletableFuture::join)
.collect(Collectors.toList());
这里简化了supplyAsync方法中的Lambda表达式,将返回值直接作为Lambda表达式的返回值,避免了创建ArrayList对象。同时使用方法引用的方式替换了flatMap中的Lambda表达式。
阅读全文