18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciexport class StringUtil {
168779efd5Sopenharmony_ci  static isEmpty(str: string): boolean {
178779efd5Sopenharmony_ci    return str == 'undefined' || !str || !/[^\s]/.test(str);
188779efd5Sopenharmony_ci  }
198779efd5Sopenharmony_ci
208779efd5Sopenharmony_ci  static removeSpace(str: string): string {
218779efd5Sopenharmony_ci    if (this.isEmpty(str)) {
228779efd5Sopenharmony_ci      return '';
238779efd5Sopenharmony_ci    }
248779efd5Sopenharmony_ci    return str.replace(/[\s]/g, '');
258779efd5Sopenharmony_ci  }
268779efd5Sopenharmony_ci
278779efd5Sopenharmony_ci  /* Obtains the result string that matches the specified substring in the original character string.
288779efd5Sopenharmony_ci   Only the result of the first successful match is returned.(The matching rule ignores spaces.)
298779efd5Sopenharmony_ci   */
308779efd5Sopenharmony_ci  static getMatchedString(textValue, regString): string {
318779efd5Sopenharmony_ci    if (this.isEmpty(textValue) || this.isEmpty(regString)) {
328779efd5Sopenharmony_ci      return '';
338779efd5Sopenharmony_ci    }
348779efd5Sopenharmony_ci    regString = this.removeSpace(regString);
358779efd5Sopenharmony_ci    let matchedTemp = '';
368779efd5Sopenharmony_ci
378779efd5Sopenharmony_ci    // spaces count
388779efd5Sopenharmony_ci    let k = 0;
398779efd5Sopenharmony_ci    for (let i = 0; i < textValue.length; i++) {
408779efd5Sopenharmony_ci      if (textValue.charAt(i) == regString.charAt(0)) {
418779efd5Sopenharmony_ci        for (let j = 0; j < regString.length; j++) {
428779efd5Sopenharmony_ci          if (textValue.charAt(i + k + j) == regString.charAt(j) || textValue.charAt(i + k + j) == ' ') {
438779efd5Sopenharmony_ci            matchedTemp = matchedTemp + textValue.charAt(i + k + j);
448779efd5Sopenharmony_ci            if (textValue.charAt(i + k + j) == ' ') {
458779efd5Sopenharmony_ci              // If the main string is a space, the substring is not counted.
468779efd5Sopenharmony_ci              k++;
478779efd5Sopenharmony_ci              j--;
488779efd5Sopenharmony_ci            }
498779efd5Sopenharmony_ci          } else {
508779efd5Sopenharmony_ci            k = 0;
518779efd5Sopenharmony_ci            matchedTemp = '';
528779efd5Sopenharmony_ci            break;
538779efd5Sopenharmony_ci          }
548779efd5Sopenharmony_ci          if (j == regString.length - 1) {
558779efd5Sopenharmony_ci            return matchedTemp;
568779efd5Sopenharmony_ci          }
578779efd5Sopenharmony_ci        }
588779efd5Sopenharmony_ci      }
598779efd5Sopenharmony_ci    }
608779efd5Sopenharmony_ci    return '';
618779efd5Sopenharmony_ci  }
628779efd5Sopenharmony_ci}