如何将数字字符串增加1 [英] How to increment a Numeric String by 1

查看:145
本文介绍了如何将数字字符串增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为OptionNumber的列,它的值类似于op0021。我想把它增加1.我可以知道我怎么样?





我想将列的最大值增加1我是这样写的



dsOptionDetails.Tables [0] .Rows [(dsOptionDetails.Tables [0] .Rows.Count) - 1] [ OptionNumber]。ToString();

I'm having a column named "OptionNumber" and it has a value like op0021. I want to increment it by 1. may i know how can i?


I want to increment the max of column by 1 that is y i've written like this

dsOptionDetails.Tables[0].Rows[(dsOptionDetails.Tables[0].Rows.Count) - 1]["OptionNumber"].ToString();

推荐答案

这里的文章正好回答了这个问题:

http://www.sqlteam.com/article/string-functions-incrementing-a-number- in-a-char [ ^ ]
There is exactly this question answered in an article here:
http://www.sqlteam.com/article/string-functions-incrementing-a-number-in-a-char[^]


如果你想在C#中这样做,我建议使用 String.Substring [ ^ ]获取 OptionNumber 的数字部分的方法。现在,使用 Int32.Parse [< a href =https://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]将string-number转换为正确数值的方法。



试试!
If you want to do that in C#, i'd suggest to use String.Substring[^] method to get numeric part of OptionNumber. Now, use Int32.Parse[^] method to convert string-number to its proper numeric value.

Try!


我真的不会建议这样做。如果您需要不断增加的序列号,请单独存储该号码。有一个单独的列包含数字,并在需要时将其递增1。根据您的示例,前缀OP看起来像是静态的,但如果不是,则将前缀存储在另一列中。但关键是要将数字放在单独的单个数字列中。这将使你的生活在未来变得更加轻松。



为了更进一步,如果数字用于唯一识别记录并使用数据库,我会建议让数据库为您生成下一个值。如果您在客户端代码中执行此操作,并将记录存储在数据库中,则两个客户端应用程序始终可能生成相同的编号,并且您将发生冲突。
I really wouldn't advice doing this. If you need a growing sequence number, store the number separately. Have a separate column containing the number and increment it by one whenever needed. Based on your example the prefix "OP" looks like a static one but if it isn't then store the prefix in another column. But the key is to have the number in a separate, single, numeric column. This will make your life much easier in the future.

To go a bit further, if the number is used to uniquely identify a record and you use a database, I would suggest letting the database to generate the next value for you. If you do it in client side code, and you store the records in a database, there is always a chance that two client applications generate the same number and you would have a collision.


这篇关于如何将数字字符串增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆