在delphi对象pascal中增加一个类似于"CJana001"的值 [英] Increment a value like 'CJana001' in delphi object pascal

查看:57
本文介绍了在delphi对象pascal中增加一个类似于"CJana001"的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库返回了一个类似于'CJana001'的值,现在我想将此值递增为'CJana002'并插入新行并保存回DB(MySql)中,此'CJana001'是字符串类型.

My DB returns me a value like 'CJana001' and now i wanted to increment this to 'CJana002' and insert a new row and save in DB(MySql) back, this 'CJana001' is of string type.

最大增量将限制在001到999的范围内,在CJana999之后不应递增.并且CJana可能会根据用户选择而有所不同.没有数字的字符串的最大长度为5,最小长度为1.

Max increment would be limited in a range of 001 to 999,It should not increment after CJana999. and the CJana may vary depending on user selection. the maximum length of the string without numbers is 5 and minimum length is 1.

CJana001可以是C_Lang001或C ++ 001或PHP001等.

CJana001 may anything like C_Lang001 or C++001 or PHP001 etc..

如何在Delphi编程中处理此问题?

How to handle this in Delphi Programming?

推荐答案

就像您也可以用任何其他语言来处理它一样:从最后一个字节开始并递增.如果已经是最高的,则将其设置为最低,并使用前面的字节重复您的工作流程.如果之前没有更多字节,请在前面添加一个具有最低值的新字节.

Like you would handle it in any other language as well: start with the last byte and increment it. If it's already the highest then set it to the lowest and repeat your workflow with the byte before. If there is no more byte before, add a new byte in front with the lowest value.

最高"和最低"是可以自由定义的-在 ASCII 世界中(并坚持使用以您的示例为例),我建议使用$ 30 .. $ 39,$ 41 .. $ 5A和$ 61 .. $ 7A.然后,当您标记为 Delphi 时,我将定义一个String常量来保存这些字节,以便您可以轻松地从头至尾进行访问.

"Highest" and "lowest" are freely definable - in an ASCII world (and sticking to your example) I suggest using $30..$39, $41..$5A and $61..$7A. And as you tagged it Delphi I'd define a String constant just holding these bytes so you can easily access them from first to last.

由于您在注释中所作的澄清,只需执行以下操作即可

thanks to your clarifications in comments it'd be easier to just do this:

var
  iNumber: Integer;
  sOld, sNew: String;
begin
  sOld:= 'CJana001';  // Wherever this comes from
  iNumber:= StrToInt( Copy( sOld, Length( sOld )- 2, 3 ) );  // Grab number part
  if iNumber< 999 then Inc( iNumber );  // Prevent overflow
  sNew:= IntToStr( iNumber );
  while Length( sNew )< 3 do sNew:= '0'+ sNew;  // Grow to 3 digits
  sNew:= Copy( sOld, 1, Length( sOld )- 3 )+ sNew;  // Prepend text part

这篇关于在delphi对象pascal中增加一个类似于"CJana001"的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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