[英]Basic String Manipulation,removal of last character in While loop
I am struggling with this basic code below , 我正在努力使用下面的基本代码,
how do i prevent the last comma "," from being appended to the String. 如何防止最后一个逗号“,”被附加到字符串。
String outScopeActiveRegionCode="";
List<String> activePersons=new ArrayList<String>();
HashSet<String> outScopeActiveRegionCodeSet=new HashSet<String>();
for (String person : activePersons) {
outScopeActiveRegionCodeSet.add(person);
}
Iterator itr = outScopeActiveRegionCodeSet.iterator();
while(itr.hasNext()){
outScopeActiveRegionCode+=itr.next();
outScopeActiveRegionCode+=",";
}
Id actually do it the other way around, id append the comma before on all cases except the first, its easier. 我实际上是以相反的方式执行,id除了第一个之外的所有情况之前都附加逗号,它更容易。
boolean isFirst = true;
while(itr.hasNext()) {
if(isFirst) {
isFirst = false;
} else {
outScopeActiveRegionCode+=",";
}
outScopeActiveRegionCode+=itr.next();
}
The reason for this is that it is much simpler to detect the first case than the last case. 这样做的原因是,检测第一种情况比最后一种情况简单得多。
I would do: 我会做:
String delimiter = "";
while(itr.hasNext()){
outScopeActiveRegionCode += delimiter;
outScopeActiveRegionCode += itr.next();
delimiter = ",";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.