在字符串末尾增加整数
我有一个字符串 Chicago-Illinos1
,我想在其末尾添加一个,因此它是 Chicago-Illinos2
。
注意:它也可能是 Chicago-Illinos10
并且我希望它转到 Chicago-Illinos11
所以我不能依赖 substr()
。
I have a string, Chicago-Illinos1
and I want to add one to the end of it, so it would be Chicago-Illinos2
.
Note: It could also be Chicago-Illinos10
and I want it to go to Chicago-Illinos11
so I can't rely on substr()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一个非常简单的问题的复杂解决方案...
如果字符串以数字结尾,它将递增数字(例如:'abc123'++ = 'abc124')。
如果字符串以字母结尾,则该字母将递增(例如:'123abc'++ = '123abd')
Complex solutions for a really simple problem...
If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').
If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')
试试这个
(现在不能尝试,但应该可以)
Try this
(can't try it now but it should work)
经过测试,有效。
Tested, works.
爆炸也可以完成这项工作
explode could do the job aswell
我将使用正则表达式来获取字符串末尾的数字(对于Java,它是[0-9]+$),增加它(int number = Integer.parse (yourNumberAsString) + 1),并与芝加哥伊利诺斯州连接(其余部分与用于查找号码的正则表达式不匹配)。
I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).
您可以使用
preg_match
来完成此操作:将输出以下内容:
但是,如果可能的话,我建议在文本和数字之间放置另一个分隔字符。例如,如果您放置了一个管道,您可以简单地执行
爆炸
并获取数组的第二部分。那就简单多了。如果字符串长度是一个问题(因此伊利诺伊州的拼写错误),您可以切换到州缩写。 (即
芝加哥-IL|1
)You can use
preg_match
to accomplish this:The following will output:
However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an
explode
and grab the second part of the array. It would be much simpler.If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e.
Chicago-IL|1
)http://php.net/manual/en/language.operators.increment。 php
http://php.net/manual/en/language.operators.increment.php
在不预先隔离数字字符串后缀的情况下,有两种选项可以增加它。
++$string
或$string++
- 一元运算符允许值的递增和递减从 PHP8.3 开始,
str_increment()
可以用稍微更稳定的行为来调用,但字符串本身必须完全是字母数字。请注意,如果您需要递增字符串数组,则使用此新的本机函数允许函数迭代器(例如array_map()
)仅通过名称调用str_increment
。 p>现在坏消息...
如果整个后缀数字通常会获得另一个数字,这两种技术都会破坏左相邻字符。例如,
a9
将增加到b0
,z99
将变为aa00
。如果这还不足以让您担心,那么还存在增加类似于浮点值的字符串值的风险。字符串值
4567e4
将递增到浮点类型值45670001.0
。阅读手册:https://www.php.net/manual/ en/language.operators.increment.php
因为那些“方便”的技术相当不可靠,所以更好的通用方法是分离、递增和替换数字后缀。
您可以通过调用
preg_replace_callback()
来避免声明临时变量。我推荐这种技术的直接性和稳定性——它总是返回一个字符串。匹配整个数字后缀,递增它,然后用递增后的后缀替换原始后缀。无副作用。代码:(演示)
要编辑字符串中任意位置的数字,只需删除
$
(字符串锚点的结尾)来自模式。Without pre-isolating the numeric string suffix, there are two options to increment it.
++$string
or$string++
- unary operators allow the incrementation and decrementation of valuesAs of PHP8.3,
str_increment()
can be called with slightly more stable behavior, but the string itself must be entirely alphanumeric. Note that if you needed to increment an array of strings, having this new native function permits functional iterators (likearray_map()
) to callstr_increment
by its name alone.Now for the bad news...
Both of these techniques will corrupt the left-neighboring character if the whole suffix number would ordinarily gain another digit. For instance,
a9
would increment tob0
andz99
would becomeaa00
.If that wasn't enough to worry you, there is also the risk of incrementing a string value that resembles a float value. The string value
4567e4
would be incremented to the float-type value45670001.0
.Read the manual: https://www.php.net/manual/en/language.operators.increment.php
Because those "convenient" techniques are rather unreliable, a better general-use approach would be to separate, increment, and replace the numeric suffix.
You can avoid declaring a temporary variable by calling
preg_replace_callback()
. I recommend this technique for directness and stability -- it will always return a string. Match the whole numeric suffix, increment it, then replace the original suffix with the incremented suffix. No side effects.Code: (Demo)
To edit a number anywhere in the string, just remove the
$
(end of string anchor) from the pattern.