简体   繁体   English

如何在长整数后附加字符?

[英]How to append character in long integer?

I want to append a character to a long integer, using the below code: 我想使用以下代码在长整数后附加一个字符:

if (strArrIds[1].Contains("CO"))
{
    long rdb2 = Convert.ToInt64(strArrIds[1].Substring(strArrIds[1].Length - 1));
    assessmentEntity.RatingType = rdb2;
}

If rdb2 = 5 , I want to append a L to this value, like rdb2 = 5L . 如果rdb2 = 5 ,我想在此值rdb2 = 5L L ,例如rdb2 = 5L

Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

You can using Long.Parse instead Convert.ToInt64 to get the long and you wont need to append L to make it long 您可以使用Long.Parse而不是Convert.ToInt64来获取long,而无需附加L来使其变长

if (strArrIds[1].Contains("CO"))
{
    long rdb2 = long.Parse(strArrIds[1].Substring(strArrIds[1].Length - 1));
    assessmentEntity.RatingType = rdb2;
}

If you are processing a lot of these, and if this item will be used for further processing, you could consider turning this into a class which might be easier to manage. 如果您正在处理大量此类文件,并且该项目将用于进一步处理,则可以考虑将其转换为可能更易于管理的类。 This would also allow you to more easily customise your ratings. 这也使您可以更轻松地自定义等级。

I'm thinking maybe a factory might serve you well also which could instantiate and return your assessment entity. 我想也许工厂也可以为您提供良好的服务,这可以实例化并返回您的评估实体。 You could then leverage a dependency injection strategy for any other functionality. 然后,您可以将依赖项注入策略用于其他任何功能。

Its a little hard to tell what you require this for without a bit more context. 在没有更多上下文的情况下很难说出您的要求。

If this is a once off, I would refactor to 如果这是一次休息,我将重构为

assessmentEntity.RatingType = strArrIds[1].Contains("CO") ? 
          String.Concat(long.Parse(strArrIds[1].Substring(strArrIds[1].Length - 1)).ToString(), "L") :
          "0N";

Assuming "0N" is some other default rating.. 假设“ 0N”是其他一些默认等级。

You do not need an L here. 您在这里不需要L That is only for literals of type long appearing in the C# source. 这仅适用于long出现在C#源代码中的文字。

You could do something like: 您可以执行以下操作:

string str1 = strArrIds[1];
if (str1.Contains("CO"))
{
    long rdb2 = str1[str1.Length - 1] - '0';
    if (rdb2 < 0L || rdb2 > 9L)
      throw new InvalidOperationException("Unexpected rdb2 value from str1=" + str1);
    assessmentEntity.RatingType = rdb2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM