php - 将特定字符向右移动
问题描述
我需要将字符串中的特定字符从特定位置向右移动特定位置。字符串、位置和地点是输入,它可以是任何值。(使用 PHP 函数最多使用较少的行数)。
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$string[($position-1)+$places] = $string[$position-1];
echo $string ;
}
预期输出是 Paceock
解决方案
with for
loop 将是一个很好的解决方案。
注意:$position
必须从1
.
<?php
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$keep = $string[$position-1];
for($i=0; $i<=$places;$i++){
$string[($position-1)+$i] = $string[$position+$i];
}
$string[$position+$places-1] = $keep;
echo $string ;
}
推荐阅读
- gdb - 如何使用 GDB 调试交叉编译的 QEMU 程序?
- java - 调用引发异常的方法的 Lambda 返回方法
- linux - 如何在空白 bash 脚本之后提取所有内容
- vue.js - 无头 CMS 和静态页面?内容更新?
- jquery - jquery ajax 在 iOS 上不起作用 - 似乎不是缓存问题
- android - 我们是否必须在 ondestroy 方法中中断后台线程?
- asterisk - 识别输入数字的 WaitExten() 问题
- mysql - 组合使用相同表和相同列的两个查询
- javascript - 为什么multer undefined?
- ruby-on-rails - 装饰者/主持人/展览和助手