程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用字符串和增量 int 生成客户 ID大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用字符串和增量 int 生成客户 ID?

开发过程中遇到使用字符串和增量 int 生成客户 ID的问题如何解决?下面主要结合日常开发的经验,给出你关于使用字符串和增量 int 生成客户 ID的解决方法建议,希望对你解决使用字符串和增量 int 生成客户 ID有所启发或帮助;

我有这个变量 字符串 Lce = "LCE"; 字符串编号 = "2005";

我想创建一个具有这种格式的客户 ID

“LCE10001-2005” 《LCE10002-2005》

如何增加整数并与这些字符串相加?我正在虑连接字符串。

解决方法

我同意约翰尼 5。基本上,每次需要时建立数字。根据您存储和检索数据的方式(即本地、数据库等),该方法会有很大差异。

以下是我总结的一些例子。

// a basic String concantenation
private static String creatEIDex1(int idNum = 0)
{
    return "LCE" + idNum + "-2005";
}

// something more flexible with optional parameters and default arguments
// utilizing a single StringBUilder object is more memory efficient
private static String creatEIDex2(StringBuilder builder = null,int idNum = 0)
{
    // initialize fields and objects
    if (builder == null)
    {
        builder = new StringBuilder();
    }
    else
    {
        builder.Clear();
    }

    String prefix = "LCE";
    String suffix = "-2005";

    // combine
    builder.Append(prefiX);
    builder.Append(idNum);
    builder.Append(suffiX);

    return builder.ToString();
}

public static void Main(String[] args)
{
    StringBuilder strb = new StringBuilder();
    String custID = "";

    // some sample,auto-incremenTing numbers
    // using example method 1
    for (int n = 10000; n < 10006; n++)
    {
        custID = creatEIDex1(n);
        Console.WriteLine(custID);
    }

    // output spacer
    Console.WriteLine();

    // using example method 2 without pasing a StringBuilder object
    for (int n = 10000; n < 10006; n++)
    {
        custID = creatEIDex2(null,n);
        Console.WriteLine(custID);
    }

    // output spacer
    Console.WriteLine();

    // using example method 2 while pasing a StringBuilder object
    for (int n = 10000; n < 10006; n++)
    {
        custID = creatEIDex2(strb,n);
        Console.WriteLine(custID);
    }

    Console.WriteLine("Press Enter key to exit");
    Console.ReadLine();
}

输出:

LCE10000-2005
LCE10001-2005
LCE10002-2005
LCE10003-2005
LCE10004-2005
LCE10005-2005

LCE10000-2005
LCE10001-2005
LCE10002-2005
LCE10003-2005
LCE10004-2005
LCE10005-2005

LCE10000-2005
LCE10001-2005
LCE10002-2005
LCE10003-2005
LCE10004-2005
LCE10005-2005
Press Enter key to exit

大佬总结

以上是大佬教程为你收集整理的使用字符串和增量 int 生成客户 ID全部内容,希望文章能够帮你解决使用字符串和增量 int 生成客户 ID所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:int