在字符串中连接一个大数

本文关键字:一个 字符串 连接 | 更新日期: 2023-09-27 18:05:42

所以我要做的是将USPS跟踪号码连接到字符串中。最后的字符串必须有带引号的跟踪号。例如,当我打印字符串时,它应该看起来像:

<tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>

我有一个跟踪号码列表作为数组(trackingIDs)中的字符串,我使用循环为每个跟踪id创建上述字符串。

 string finalString = null;
    for(int i = 0; i < trackingIDs.length; i++)
    {
        finalString = "<tracking userID=ABC trackingID='"" + trackingIDs[i] + "'" deliveryNotification=false>";
          Console.WriteLine(finalString);
    }

这段代码产生了:

<tracking userID=ABC trackingID='"910236783468367367346738'" deliveryNotification=false>

对于较小的数字,我可以简单地将其转换为整数,一切看起来都很好。但我认为这些追踪号码超过了长时间的容量,所以转换是不合理的。我的问题是如何让这个字符串中的"'"斜杠消失

在字符串中连接一个大数

根据您的代码,它应该正确转义。我认为你们可能只是在调试器中查看它,为了方便你们观看,它会省略引号。

另一种不带'转义字符的格式化字符串的方法如下:

finalString = string.Format(@"<tracking userID=ABC trackingID=""{0}"" deliveryNotification=false>", trackingIDs[i]);

另外,你可以用单引号代替。

finalString = string.Format(@"<tracking userID=ABC trackingID='{0}' deliveryNotification=false>", trackingIDs[i]);

我将而不是建议使用数字类型来存储您的跟踪号码。

您的代码中没有任何问题,因为它是正确的。您看到这是因为您将鼠标移到Visual Studio中显示转义字符的变量上。

这没什么问题。使用你的代码(和大写长度)我得到这个。

var trackingIDs = new string[]{"910236783468367367346738"};
 string finalString = null;
for(int i = 0; i < trackingIDs.Length; i++)
{
    finalString = "<tracking userID=ABC trackingID='"" + trackingIDs[i] + "'" deliveryNotification=false>";
      Console.WriteLine(finalString);
}
输出:

 <tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>

这个怎么样?

string[] trackingIDs = {"910236783468367367346738","810236783468367367346738","710236783468367367346738"};
    string finalString = null;
    for(int i = 0; i < trackingIDs.Length; i++)
    {
        finalString = "<tracking userID=ABC trackingID='"" + trackingIDs[i] + "'" deliveryNotification=false>";
        Console.WriteLine(finalString);
    }

这是我得到的输出:

<tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>
<tracking userID=ABC trackingID="810236783468367367346738" deliveryNotification=false>
<tracking userID=ABC trackingID="710236783468367367346738" deliveryNotification=false>

尝试不同的方法。如果您首先取出UPS跟踪ID并将其放入List中,然后旋转这些ID并使用string. format()构建最终字符串,该怎么办?在你的例子中,我认为你没有正确地转义引号…

就像这样。

var trackingId = "123456789098888888893233432423423";
var finalString = string.Format("<tracking userID=ABC trackingID='"{0}'" deliveryNotification=false>", trackingId);

在c#中,双引号使用另一个转义:

参考这个问题:我可以转义字串字面值中的双引号吗?

所以,你不需要使用反斜杠