这个问题的基础来自FLEX映射应用程序,在该应用程序中,我正在使用小部件中的复选框来为要素图层中服务的地理空间数据库中的多个字段生成查询字符串。在程序为数据库中的每个可用字段编译了每个查询之后,我坚持将每个查询连接起来。这在函数结尾处显示如下。这是遍历复选框,生成查询字符串并尝试连接查询字符串的函数:
public function compileDefexp():void {
//conFilter is an HBox that contains the available field names in checkboxes.
var countfilt:uint = conFILTER.numElements;
var defExpGEO_ACC:String = "";
var defExpACT_TYP:String = "";
var defExp:Array = [];
//clearing filterList on each checkbox click
filterList = [];
//loop through the field and field value checkboxes to generate the query strings
for(var i:int = 0;i < countfilt; i++){
var childFilter:Object = conFILTER.getElementAt(i);
var filterName:String = childFilter.name;
if(childFilter.selected){
filterList.push(filterName);
if(filterName == "GEO_ACC"){
compileValuelistgeo();
defExpGEO_ACC = "(" + filterName + ") IN (" + qValuelistGEO_ACC + ")";
defExp.push(defExpGEO_ACC);
}else if(filterName == "ACT_TYP"){
compileValuelistact();
defExpACT_TYP = "(" + filterName + ") IN (" + qValuelistACT_TYP + ")";
defExp.push(defExpACT_TYP);
}
}
}
var defExpStr:String = defExp.toString();
var countfilterList:uint = filterList.length;
var filterListstr:String = filterList.toString();
//Cannot get replace the comma between the closed and open parentheses with AND
//We need to accomplish this to concatenate the query strings for the definition
//expression to execute correctly.
//Tried to use RegExp
var strPattern:RegExp = /\),\(/g;
var strReplace:RegExp = /\) AND \(/g;
defExpStr.replace(strPattern, strReplace);
geoPoints.definitionExpression = defExpStr;
trace(defExpStr); //(GEO_ACC) IN (GA Value 1,GA Value 2),(ACT_TYP) IN (AT Value 1)
//We need this to be:
//(GEO_ACC) IN (GA Value 1,GA Value 2) AND (ACT_TYP) IN (AT Value 1)
}
最佳答案
您可以像这样使用replace函数:
public function replaceExample(str:String, patternOld:String, patternNew:String):String
{
var str2:String = str.replace(patternOld,patternNew);
return str2;
}
//Test result will be:(GEO_ACC) IN (GA Value 1,GA Value 2) AND (ACT_TYP) IN (AT Value 1)
var str:String = "(GEO_ACC) IN (GA Value 1,GA Value 2),(ACT_TYP) IN (AT Value 1)";
Alert.show( replaceExample(str, "),(", ") AND (") );