Tag: 字符串

将String转换为字符串数组的最快方法

该脚本必须validation大量IP中是否存在一个预定义的IP。 目前我的代码function就像这样(说“ips”是我的IP数组,“ip”是预定义的ip) ips.each do |existsip| if ip == existsip puts “ip exists” return 1 end end puts “ip doesn’t exist” return nil 有没有更快的方法来做同样的事情? 编辑:我可能错误地表达了自己。 我可以做array.include吗? 但我想知道的是:array.include? 能给我最快结果的方法吗?

如何找出ruby中每场比赛的起点

说,我有一个跟随字符串 string = “#Sachin is Indian cricketer. #Tendulkar is right hand batsman. #Sachin has been honoured with the Padma Vibhushan award ” 我想要o / p为 “#Sachin|0|7;#Tendulkar|29|10;#Sachinn|63|7;” 我试过跟随 new_string = “” string.scan(/#\S+/).each{|match| new_string+=”#{match}|#{string.index(match)}|#{match.length};” } 这给了我 “#Sachin|0|7;#Tendulkar|29|10;#Sachin|0|7;” 那么我将如何获得每个子字符串的起始索引?

Ruby:删除字符串开头的空格字符

我有一个单词数组,我试图删除可能存在于单词开头而不是结尾的任何空格。 rstrip! 只需要处理字符串的结尾。 example_array = [‘peanut’, ‘ butter’, ‘sammiches’] desired_output = [‘peanut’, ‘butter’, ‘sammiches’] 正如您所看到的,并非数组中的所有元素都有空白问题,因此如果所有元素都以单个空格字符开头,我就不能像删除第一个字符一样删除第一个字符。 完整代码: words = params[:word].gsub(“\n”, “,”).delete(“\r”).split(“,”) words.delete_if {|x| x == “”} words.each do |e| e.lstrip! end 用户可以在表单上输入的示例文本: Corn on the cob, Fibonacci, StackOverflow Chat, Meta, About Badges Tags,, Unanswered Ask Question

在流浪汉配置期间使用Sed将行附加到文件

我目前正在使用Vagrant来设置运行Ubuntu的开发机器。 我想在我的.profile中添加几行,使用sed将目录添加到我的$PATH变量中。 为此,我将这些行添加到我的机器的Vagrantfile : config.vm.provision “shell”, inline: “sudo sed -i \’$ a if [ -d \\\”/usr/local/lib\\\” ]; then\n PATH=\\\”/usr/local/lib:$PATH\\\”\nfi\’ /home/vagrant/.profile” 据我所知和测试,我已经逃脱了让Vagrant运行这个有效的shell命令所需的所有角色: sudo sed -i ‘$ a if [ -d \”/usr/local/lib\” ]; then\n PATH=\”/usr/local/lib:$PATH\”\nfi’ /home/vagrant/.profile 这将这些行添加到我的.profile : if [ -d “usr/local/lib” ]; then PATH=”usr/local/lib:$PATH” fi 但是,当我做一个vagrant up我在尝试运行命令时收到以下错误: ==> default: Running provisioner: shell… default: Running: inline script […]

ruby to unjumble words

我正在尝试编写一个ruby脚本,解读置换的单词,生成所有排列,并在txt目录中搜索单词。 我遇到了问题。 这是我所拥有的简单概述。 print “Enter Permuted Word:” words = STDIN.gets.chomp a = to_array(s) print a, “\n” perms = a.permutation(3).to_a.collect do |perm| perm.join end print perms, “\n” file = file.open(“words.txt”, “r”) file.read.each_line do |line| fields = line.chomp.split(“,”) words_un = fields[1] end file.close txt文件看起来像这样 words_un Aarhus Aaron Ababa aback abaft abandon abandoned abandoning abandonment abandons abase … […]

为什么在读取文本文件时出现“UTF-8中的无效字节序列”错误?

我正在编写一个Ruby脚本来处理一个大文本文件,并继续得到奇怪的编码错误。 这是情况: input_data = File.new(in_path, ‘r’).read p input_data.encoding.name # UTF-8 break_char = “\r”.encode(“UTF-8”) p break_char # “\r” p break_char.encoding.name # “UTF-8” input_data.split(“,”.encode(“UTF-8”)) p Encoding.compatible?(input_data, break_char) # # Encoding:UTF-8> 这会产生错误:in ‘split’: invalid byte sequence in UTF-8 (ArgumentError) 我阅读http://blog.grayproductions.net/articles/ruby_19s_string并查看其他解决方案显然是同样的问题,但仍然无法解决为什么它发生在我相信我控制编码时。 我在OSX上使用ruby 1.9.2

ruby字符串到哈希转换

我有一个像这样的字符串, str = “uu@p, xx@m, yy@n, zz@m” 我想知道如何将给定的字符串转换为哈希。 (即我的实际要求是,有多少个值(在@符号之前)有m,n和p。我不想计数,我需要一个确切的值)。 输出会更好像这样, {“m” => [“xx”, “zz”], “n” => [“yy”], “p” => [“uu”]} 可以帮帮我吗?

如何替换ruby字符串中的文本

我试图在Ruby中编写一个非常简单的方法,它接受一个字符串和一个单词数组,并检查字符串是否包含任何单词,如果是,它将用大写字母替换它们。 我做了一个尝试,但由于我的Ruby技能水平,它并不是很好。 def(my_words,my_sentence) #split the sentence up into an array of words my_sentence_words = my_sentence.split(/\W+/) #nested loop that checks the words array for each brand my_sentence_words.each do |i| my_words.each do |i| #if it finds a brand in the words and sets them to be uppercase if my_words[i] == my_sentence_words[i] my_sentence_words[i] == my_sentence_words[i].up.case end end end […]

字符串整数

假设您有字符串”Hi” 。 你如何得到8的值( “H”是字母表的第8个字母, “i”是第9个字母)。 然后说,在这些整数中加1并使其成为9然后可以将其重新组成字符串”Ij” ? 可能吗?

用于修改字符串外部引号的ruby代码?

有没有人知道在字符串的外部引号上运行的Ruby gem(或内置或本机语法)? 我发现自己一遍又一遍地写这样的方法: remove_outer_quotes_if_quoted( myString, chars ) -> aString add_outer_quotes_unless_quoted( myString, char ) -> aString 第一个测试myString以查看其开头和结尾字符是否与字符中的任何一个字符匹配。 如果是这样,它将返回删除了引号的字符串。 否则它会保持不变。 chars默认为引号字符列表。 第二个测试myString以查看它是否已经开始并以char结尾。 如果是这样,它将返回不变的字符串。 如果没有,它返回前后加上char的字符串,并且使用反斜杠转义任何嵌入的char字符串。 char默认为默认字符列表中的第一个。 (当然,我的手工鹅卵石方法没有这么详细的名字。) 我在公共回购中寻找类似的方法,但找不到这样的东西。 我是唯一需要这么做的人吗? 如果没有,其他人如何做到这一点?