寻找在Swift中从字符串中过滤文本的最佳方法

时间:2019-06-27 06:39:59

标签: swift data-structures

我有一个按升序排列的字符串数组。我想过滤该数组中的char / text,并首先从搜索到的文本字母中获取结果,然后再从其余部分获取结果。我正在寻找最简单的方法来完成这项工作。 示例:

declare
 type t_ntt is table of test1%rowtype index by pls_integer;
 l_ntt t_ntt;
 c_limit INTEGER := 100;
 sqltext VARCHAR2(1000);
 table_name VARCHAR2(30) := 'test1';
 column_name VARCHAR2(30) := 'A';
 c_cursor sys_refcursor; 
begin
  open c_cursor for 'select '|| column_name|| ' from ' || table_name ;
  loop
    fetch c_cursor bulk collect into l_ntt limit c_limit;
    exit when l_ntt.count = 0;
    dbms_output.put_line(l_ntt.count);
    forall i in indices of l_ntt
    insert into test values l_ntt(i);

  end loop;
  close c_cursor;

end;

且搜索文本为“ R”

输出应为:

Var array = ["Anand", "Ani", "Dan", "Eion", "Harsh", "Jocab", "Roshan", "Stewart"]

4 个答案:

答案 0 :(得分:2)

执行此操作的一种方法是首先将字符串映射到一个元组,该元组包含字符串中搜索文本的索引以及字符串本身。然后按索引排序,然后将元组映射回字符串。

let array = ["Anand", "Ani", "Dan", "Eion", "Harsh", "Jocab", "Roshan", "Stewart"]
let searchText = "R"
// compactMap acts as a filter, removing the strings where string.index(of: searchText, options: [.caseInsensitive]) returns nil
let result = array.compactMap { string in string.index(of: searchText, options: [.caseInsensitive]).map { ($0, string) } }
                    .sorted { $0.0 < $1.0 }.map { $0.1 }

index(of:options:)方法来自this answer here

对于Swift 4.x:

extension StringProtocol where Index == String.Index {
    func index(of string: Self, options: String.CompareOptions = []) -> Index? {
        return range(of: string, options: options)?.lowerBound
    }
    func endIndex(of string: Self, options: String.CompareOptions = []) -> Index? {
        return range(of: string, options: options)?.upperBound
    }
    func indexes(of string: Self, options: String.CompareOptions = []) -> [Index] {
        var result: [Index] = []
        var startIndex = self.startIndex
        while startIndex < endIndex,
            let range = self[startIndex...].range(of: string, options: options) {
                result.append(range.lowerBound)
                startIndex = range.lowerBound < range.upperBound ? range.upperBound :
                    index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
        }
        return result
    }
    func ranges(of string: Self, options: String.CompareOptions = []) -> [Range<Index>] {
        var result: [Range<Index>] = []
        var startIndex = self.startIndex
        while startIndex < endIndex,
            let range = self[startIndex...].range(of: string, options: options) {
                result.append(range)
                startIndex = range.lowerBound < range.upperBound ? range.upperBound :
                    index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
        }
        return result
    }
}

答案 1 :(得分:1)

filter上使用 array 来过滤所有包含Strings的{​​{1}},即

searchText

答案 2 :(得分:0)

尝试一下


 if searchText.!characters.isEmpty
 {
      for strSearch in filterArray
      {
           let range = strSearch.lowercased().range(of: searchText, options: .caseInsensitive, range: nil,   locale: nil)

           if range != nil
            {
               searchedArray.append(strSearch)
            }
        }
     } 
  else 
  {
      searchedArray = filterArray
  }

答案 3 :(得分:0)

let words = ["Anand", "Ani", "Dan", "Eion", "Harsh", "Jocab", "Roshan", "Stewart"]
let keyword = "r"
let result = words.filter { $0.contains(keyword) }
    .sorted { ($0.hasPrefix(keyword) ? 0 : 1) < ($1.hasPrefix(keyword) ? 0 : 1) }