我有一个按=
参数拆分的网址,我想做的是在每个=
之后将字符插入数组:
例如:
http://multiplexstimulator.com/catalog/product.php?cat_id=82&pid=157&view=print
应该用等号分成一个如下所示的数组:
["http://multiplexstimulator.com/catalog/product.php?cat_id", "=", "82&pid", "=", "157&view", "=", "print"]
然后我想添加字母" a"在等号后:
["http://multiplexstimulator.com/catalog/product.php?cat_id", "=", "a", "82&pid", "=", "a", "157&view", "=", "a", "print"]
完成此操作后,我想要join
输出该数组:
"http://multiplexstimulator.com/catalog/product.php?cat_id=a82&pid=a157&view=aprint"
我尝试过的事情:
module MultipleParameters
class TestAllParameters
def check_for_multiple_parameters(site, char)
site_arr = []
if site.count("=") != 1
site.split("=").each do |string_split|
site_arr.push(string_split)
end
end
inject_syntax(site_arr, char)
end
def inject_character(array, char)
array.each do |param|
param.inject(char)
end
puts array.join("")
end
end
end
然而,当我这样做时,我收到了错误:
detection.rb:18:in `block in inject_syntax': undefined method `inject' for "http://multiplexstimulator.com/catalog/product.php?cat_id":String (NoMethodError)
Did you mean? inspect
from detection.rb:17:in `each'
from detection.rb:17:in `inject_syntax'
from detection.rb:13:in `check_for_multiple_parameters'
from detection.rb:33:in `<main>'
如何成功完成此操作?我是否需要scan
字符串并将其拆分为&#34; =&#34;找到而不是用等号分割字符串?
答案 0 :(得分:3)
您可以使用#include <stdio.h>
#include <string.h>
#include <omp.h>
#define N 20
int main() {
int x[N+1];
for(int i=0; i<N; i++) x[i] = i+1;
for(int i=0; i<N; i++) printf("%d ", x[i]); puts("");
#pragma omp parallel
{
int ithread = omp_get_thread_num(), nthreads = omp_get_num_threads();
size_t pos = ithread*N/nthreads;
size_t n = (ithread+1)*N/nthreads - pos - 1;
int tmp = x[pos+n];
memmove(&x[pos+1], &x[pos], sizeof *x*n);
#pragma omp barrier
x[pos+n+1] = tmp;
}
for(int i=0; i<N+1; i++) printf("%d ", x[i]); puts("");
}
方法。
gsub
这会产生
str = 'http://multiplexstimulator.com/catalog/product.php?cat_id=82&pid=157&view=print'
str.gsub('=','=a')
请注意"http://multiplexstimulator.com/catalog/product.php?cat_id=a82&pid=a157&view=aprint"
不会修改原始字符串,如果要修改它,请使用gsub
答案 1 :(得分:2)
首先,您应该将params与url分开
url = 'http://multiplexstimulator.com/catalog/product.php?cat_id=82&pid=157&view=print'
array = url.split('/')
现在你应该改变所有&#39; =&#39;到&#39; = a&#39;在此数组的最后一个元素
array[-1] = array[-1].gsub('=', '=a')
现在你可以在这个数组上使用join:)
new_url = array.join('')