拆分字符串而不删除分隔符 [英] Split string without removing delimiter
本文介绍了拆分字符串而不删除分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析一个文件以获取单个 SQL 语句并从 rails 控制器运行它们.
I need to parse a file to get individual SQL statements and run them from rails controller.
我有以下代码:
@sql_file = "#{RAILS_ROOT}/lib/evidence_interface_import.sql"
@sql_stmts_array = File.read(@sql_file).split(";")
@sql_stmts_array.each_with_index do |sql_stmt,s_index|
ActiveRecord::Base.connection.execute(sql_stmt)
end
拆分删除了;"从 SQL 的末尾开始.有没有办法不删除;"仍然使用;"分割.
The split removes the ";" from the end of the SQLs. Is there a way not to remove the ";" and still split using ";".
推荐答案
这有效:
@sql_stmts_array = File.read(@sql_file).lines(separator=';')
这篇关于拆分字符串而不删除分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文