加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 单词的子字符串

发布时间:2020-12-15 18:23:41 所属栏目:百科 来源:网络整理
导读:文本: Sed ut perspiciatis unde omnis iste natus error sit voluptatem ac 我想像word.Substring(1,29)那样以不规则的方式对单词进行子串. 常规方式: “Sed ut perspiciatis unde om” 但我想要: “Sed ut perspiciatis unde” 所以只显示完整的单词.如
文本:

Sed ut perspiciatis unde omnis iste natus error sit voluptatem ac

我想像word.Substring(1,29)那样以不规则的方式对单词进行子串.

常规方式:

“Sed ut perspiciatis unde om”

但我想要:

“Sed ut perspiciatis unde”

所以只显示完整的单词.如果在一个单词内被切断之前将显示.希望了解我在寻找什么.

解决方法

public static String ParseButDontClip(String original,int maxLength)
{
    String response = original;

    if (original.Length > maxLength)
    {
        int lastSpace = original.LastIndexOf(' ',original.Length - 1,maxLength);
        if (lastSpace > -1)
            response = original.Substring(0,lastSpace);
    }
    return response;
}

String.LastIndexOf第二个参数实际上是要搜索的子字符串的END – 最长的是你需要走多远的开始.

每次我使用它都会得到我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读