C#替换数组之间的字符串 [英] C# replacing string between arrays

查看:277
本文介绍了C#替换数组之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我不觉得这个问题是不言自明的,所以我会解释一下。

我正在制作一个需要重新定位数据的程序,但是它逐个读取一行所以我需要存储要更换的东西并用数组替换它。

让我说我需要更换字符串

test

with

test2

我的数组会这样设置:

Ok I don't feel like the question is self-explanatory so I will explain it.
I'm making a program that requires relocating the data, but it reads a line one by one so I need to store things to replace and to replace it with in a array.
Lets say I need to replace the string
"test"
with
"test2"
well my array would be set up like this:

string[] arr = new string[10];
arr[1] = "test";
arr[2] = "test2";





这就是设置。

这是我的代码:



That would be the setup.
Here's my code for this:

string text = kek.Text;

for (int idx = 0; idx < reloc.Length; idx++)
{
    text = Regex.Replace(text, reloc[idx], reloc[idx + 1]);
}
return text;





我我想知道如何正确地做到这一点,因为我现在得到的错误是:

值不能为空参数名称模式



我尝试过:



重写我的方法。

用简单的数学调整。



I was wondering how would I do this correctly as the error I'm getting right now is:
"Value Cannot be Null Parameter Name Pattern"

What I have tried:

Rewriting my method.
Adjusting with simple math.

推荐答案

以下是基于您提供的内容的示例。假设reloc数组中只有两个虚拟值。但循环试图循环10次(数组的长度)。代码将投诉,因为它试图从reloc [2],reloc [3],...中检索不可用的值。再一次,这只是基于此处所呈现的内容的假设。



C#Online Compiler | .NET小提琴 [ ^ ]
Here is an example based on what you had provided. Assuming there are only two dummy value in the reloc array. But the loop is trying to loop 10 times (length of the array). The code will complaint because it try to retrieve the value from reloc[2], reloc[3], ...which is not available. Again that just my assumption based on what being presented here.

C# Online Compiler | .NET Fiddle[^]


这篇关于C#替换数组之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆