C# 重复字符串,行内略有变化

发布于 2025-01-02 18:22:29 字数 409 浏览 2 评论 0原文

例如我有这样的数组字符串。下面是示例 [0] 的字符串

Name\r\n
Gamma\r\n
ID\r\n
3F97\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n

,我想将此字符串中的值复制到示例 [0] 中,

Name\r\n
Gamma\r\n
ID\r\n
3F97\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n
Name\r\n
Gamma\r\n
ID\r\n
3F98\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n

无论如何可以做到这一点吗? 请注意,更改位于 ID 下方,从 3F97 更改为 3F98

For example i have strings of array like this. Below is the string of Example[0]

Name\r\n
Gamma\r\n
ID\r\n
3F97\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n

well i would like to duplicate this value from this string into Example[0]

Name\r\n
Gamma\r\n
ID\r\n
3F97\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n
Name\r\n
Gamma\r\n
ID\r\n
3F98\r\n
CAR\r\n
Mitsubishi EVO LAN V\r\n

is there anyway to do this?
note that the change is below the ID from 3F97 into 3F98

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2025-01-09 18:22:29

您可以按行尾拆分字符串,更改第四行,将其连接并将其连接到第一个字符串。

You can split the string by end-of-line, change the fourth line, join it and concatenate it to the first string.

风蛊 2025-01-09 18:22:29

我认为你的问题可能会给人错误的印象,如果我错了请纠正我,但你这里有一个定义“汽车”的类。

那么为什么不创建一个实际的类来表示汽车并实现 ToString() 方法呢?

public class Car {

    public string Name {get;set}
    public string ID {get;set;}
    public string CAR {get;set;}

    public Car(string name,string id,string car){
        Name = name;
        ID = id;
        CAR = car;
    }

    public override string ToString(){
        return string.format("Name: {0}, ID: {1}, CAR: {2}",Name,ID,CAR);
    }
}

然后,使用汽车数组代替字符串数组,您可以随意复制该数组,并在必要时进行修改。

您还将有一个修改输出格式的中心位置,即 Car 类定义(即使在正常情况下,我永远不会推广像这样的 ToString() 方法用于输出,除了指向记录器)

I think your question might be giving the wrong impression, correct me if I'm wrong, but what you have here is a class that defines a 'car'.

Why then don't you just create an actual class to represent a car and implement a ToString() method?

public class Car {

    public string Name {get;set}
    public string ID {get;set;}
    public string CAR {get;set;}

    public Car(string name,string id,string car){
        Name = name;
        ID = id;
        CAR = car;
    }

    public override string ToString(){
        return string.format("Name: {0}, ID: {1}, CAR: {2}",Name,ID,CAR);
    }
}

Then, instead of an array of strings, have an array of cars, which you can duplicate as you please and modify if necessary.

You will also have a central place to modify the format of the output, i.e. the Car class definition (even though in normal circumstances, I would never promote a ToString() method like this for output, other than that directed to a logger)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文