字符串超过特定长度使用...代替

function intercept($str,$length = 10){
    if (strlen($str)>$length)
        $str=substr($str,0,$length) . '...';
    return $str;
}

 

转载于:https://www.cnblogs.com/pcx105/p/9316046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用字符串替换函数来实现这个功能,例如在 C 语言中可以使用函数str_replace()。具体实现方法如下: 1. 定义两个字符串变量,一个用于存储原始字符串,一个用于存储替换后的字符串。 2. 使用fgets()函数从标准输入中读取原始字符串。 3. 使用str_replace()函数将原始字符串中的特定字符串替换为短字符串。 4. 将替换后的字符串输出到标准输出。 下面是一个示例代码: #include <stdio.h> #include <string.h> char* str_replace(char* orig, char* rep, char* with) { char* result; // 替换后的字符串 char* ins; // 插入位置 char* tmp; // 临时变量 int len_rep; // 替换字符串长度 int len_with; // 替换后的字符串长度 int len_front; // 前缀字符串长度 int count; // 计数器 if (!orig || !rep) { return NULL; } len_rep = strlen(rep); if (len_rep == ) { return NULL; } if (!with) { with = ""; } len_with = strlen(with); ins = orig; for (count = ; tmp = strstr(ins, rep); ++count) { ins = tmp + len_rep; } tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); if (!result) { return NULL; } while (count--) { ins = strstr(orig, rep); len_front = ins - orig; tmp = strncpy(tmp, orig, len_front) + len_front; tmp = strcpy(tmp, with) + len_with; orig += len_front + len_rep; } strcpy(tmp, orig); return result; } int main() { char orig[100]; // 原始字符串 char rep[100]; // 要替换的字符串 char with[100]; // 替换后的字符串 printf("请输入原始字符串:"); fgets(orig, sizeof(orig), stdin); orig[strcspn(orig, "\n")] = ; // 去掉换行符 printf("请输入要替换的字符串:"); scanf("%s", rep); printf("请输入替换后的字符串:"); scanf("%s", with); char* result = str_replace(orig, rep, with); printf("替换后的字符串为:%s\n", result); free(result); return ; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值