ESQL没有像Java这样的内置字符串拆分函数,尽管它很容易构建静态函数并将* .jar添加到IIB类路径中,但我工作过的几个站点都禁止使用Java。< / p>
那么ESQL中高效的字符串分隔符是什么样子。
答案 0 :(得分:2)
主题的以下四个变体可用于拆分ESQL字符串。
我没有选择添加很多参数并使用相当复杂的内部逻辑作为结束,而是选择了使用函数名而不是标志的选项。
SplitString
不添加空字符串,而是添加多个空格的字符串。
CREATE PROCEDURE SplitString(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR CompositeString;
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
DECLARE Element CHAR SUBSTRING(Remainder BEFORE Delimiter);
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
DECLARE Element CHAR Remainder;
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = '';
END IF;
END WHILE;
END;
SplitStringTrim
修剪元素字符串中的前导和尾随空白。
不添加空字符串或空字符串。
CREATE PROCEDURE SplitStringTrim(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR TRIM(CompositeString);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
DECLARE Element CHAR TRIM(SUBSTRING(Remainder BEFORE Delimiter));
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = SUBSTRING(Remainder AFTER Delimiter;
ELSE
DECLARE Element CHAR TRIM(Remainder);
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = '';
END IF;
END WHILE;
END;
SplitStringAddEmpty
将空元素添加到SplitterArray,确保至少有一个元素。
保留空白。
CREATE PROCEDURE SplitStringAddEmpty(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR CompositeString;
DECLARE EndsWithDelimiter BOOLEAN ENDSWITH(Remainder, Delimiter);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
IF LENGTH(Remainder) = 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
ELSE
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE SUBSTRING(Remainder BEFORE Delimiter);
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Remainder;
SET Remainder = '';
END IF;
END WHILE;
IF EndsWithDelimiter THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
END IF;
END IF;
END;
SplitStringAddEmptyTrim
将空元素添加到SplitterArray,确保至少有一个元素。
修剪Element字符串中的前导和尾随空白。
CREATE PROCEDURE SplitStringAddEmptyTrim(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR TRIM(CompositeString);
DECLARE EndsWithDelimiter BOOLEAN ENDSWITH(Remainder, Delimiter);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
IF LENGTH(Remainder) = 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
ELSE
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE TRIM(SUBSTRING(Remainder BEFORE Delimiter));
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE TRIM(Remainder);
SET Remainder = '';
END IF;
END WHILE;
IF EndsWithDelimiter THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
END IF;
END IF;
END;