C# - 重复更改
我有一个如下所示的文件:
R.D. P.N. X Y Rot Pkg
L5 120910 64.770 98.425 180 SOP8
P4 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
P4 120911 25.400 83.820 180 0603
L5 120910 62.484 98.425 180 SOP8
L5 120910 99.100 150.105 180 SOP8
.. ...... ...... ...... .. .......
我想使用 string.Split() 分割每个字符串,然后检查每行上“string[0]”的第一个值。如果相同的字符串[0]有重复项,我想在字符串[0]的末尾添加一个递增的“-#”。所以它会类似于 string[0] + "-i"
.....
我的意思是对于 .txt 中的 L5
在上面,它们将更改为 L5-1
、L5-2
、L5-3
。 P4
也是如此(即 P4-1
、P4-2
)......
因此新的 .txt 将看起来像这样:
R.D. P.N. X Y Rot Pkg
L5-1 120910 64.770 98.425 180 SOP8
P4-1 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
P4-2 120911 25.400 83.820 180 0603
L5-2 120910 62.484 98.425 180 SOP8
L5-3 120910 99.100 150.105 180 SOP8
.. ...... ...... ...... .. .......
问题:
- 我怎样才能去做这样的事情?
- 对理解如何做到这一点有任何帮助吗?
I have a file that looks like this:
R.D. P.N. X Y Rot Pkg
L5 120910 64.770 98.425 180 SOP8
P4 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
P4 120911 25.400 83.820 180 0603
L5 120910 62.484 98.425 180 SOP8
L5 120910 99.100 150.105 180 SOP8
.. ...... ...... ...... .. .......
I would like to use string.Split()
to split each string up and then check the first value of the "string[0]" on every line. If there are duplicates of the same string[0] I would like to add an incremented "-#" to the end of the string[0]. So it would be something like string[0] + "-i"
.....
What I mean by this is that for the L5
's in the .txt above, they would be changed to L5-1
,L5-2
,L5-3
. The same goes with the P4
's (ie, P4-1
,P4-2
)....
So the new .txt would look like this:
R.D. P.N. X Y Rot Pkg
L5-1 120910 64.770 98.425 180 SOP8
P4-1 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
P4-2 120911 25.400 83.820 180 0603
L5-2 120910 62.484 98.425 180 SOP8
L5-3 120910 99.100 150.105 180 SOP8
.. ...... ...... ...... .. .......
QUESTIONS:
- How can I go about doing such a thing?
- Any help on understand how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您可以接受仅包含一次出现的所有值的 -1,这里有一个示例程序。您必须在最后制作一份副本,因为在写入新文件时无法删除原始文件。
示例输入:
示例输出(已测试):
如果您不想要 -1,您始终可以在写入之前进行检查以确保计数 > 1.
If you are ok with having a -1 for all values that only contain one occurrence, here is a samples program. You'd have to do a copy at the end, since you can't delete the original file while writing the new one.
Sample input:
Sample output (tested):
If you don't want the -1, you can always do a check before the write to ensure that the count is > 1.
我会怎么做:
编辑字典示例:
如果您知道字典已经包含该键,则应该仅使用
dictionary[string]
语法,否则您可能会遇到错误。另一种方法是使用 TryGetValue 方法:How I would do it:
Edit for dictionary example:
You should only use the
dictionary[string]
syntax if you know the dictionary already includes that key, otherwise you risk getting an error. Another way to do it is to use the TryGetValue method:这个怎么样:
这会给你你想要的结果。可能有更有效的方法,但是,对于 800 行来说,这应该足够了。
How about this one:
This will give you the result you are after. There may be more efficient ways, but, for 800 lines this should be good enough.
800 行是相当小的。
遍历数组,并将第一列添加到字典中。在添加到字典之前测试一下该值是否出现在其中。如果是,只需增加计数器即可。
查一下你的字典。对于每个值大于 1 的键,循环数组并将“-1”、“-2”等附加到该键的所有出现位置。 ..
效率不是很高,但很容易实现
800 lines is pretty tiny.
Go through the array, and add the first column to a dictionary. Before adding to the dictionary test to see if the value apperas in it. If it does, just increment the counter.
Go through your dictionary. For each key whose value is greater than 1, cycle the array and append "-1", "-2", etc to all occurrences of the key. ..
Not very efficient, but pretty easy to implement