字符串的截取(公共方法)

 1 #region 从字符串的指定位置截取指定长度的子字符串
 2         /// <summary>
 3         /// 从字符串的指定位置截取指定长度的子字符串
 4         /// </summary>
 5         /// <param name="str">原字符串</param>
 6         /// <param name="startIndex">子字符串的起始位置</param>
 7         /// <param name="length">子字符串的长度</param>
 8         /// <returns>子字符串</returns>
 9         private static string CutString(string str, int startIndex, int length)
10         {
11             if (startIndex >= 0)
12             {
13                 if (length < 0)
14                 {
15                     length = length * -1;
16                     if (startIndex - length < 0)
17                     {
18                         length = startIndex;
19                         startIndex = 0;
20                     }
21                     else
22                     {
23                         startIndex = startIndex - length;
24                     }
25                 }
26 
27                 if (startIndex > str.Length)
28                 {
29                     return "";
30                 }
31             }
32             else
33             {
34                 if (length < 0)
35                 {
36                     return "";
37                 }
38                 else
39                 {
40                     if (length + startIndex > 0)
41                     {
42                         length = length + startIndex;
43                         startIndex = 0;
44                     }
45                     else
46                     {
47                         return "";
48                     }
49                 }
50             }
51 
52             if (str.Length - startIndex < length)
53             {
54                 length = str.Length - startIndex;
55             }
56 
57             return str.Substring(startIndex, length);
58         }
59        
60         /// <summary>
61         /// 从字符串的指定位置开始截取到字符串结尾的了符串
62         /// </summary>
63         /// <param name="str">原字符串</param>
64         /// <param name="startIndex">子字符串的起始位置</param>
65         /// <returns>子字符串</returns>
66         private static string CutString(string str, int startIndex)
67         {
68             return CutString(str, startIndex, str.Length);
69         }
70         #endregion

 

转载于:https://www.cnblogs.com/hugeboke/p/11574677.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值