截取字符串函数

截取字符串函数:解决了中文与英文截取不同的问题。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1ContractedBlock.gifExpandedBlockStart.gif 字符串截取函数#region 字符串截取函数
 2InBlock.gif    public static string CutString(string inputString, int len)
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 4InBlock.gif
 5InBlock.gif        ASCIIEncoding ascii = new ASCIIEncoding();
 6InBlock.gif        int tempLen = 0;
 7InBlock.gif        string tempString = "";
 8InBlock.gif        byte[] s = ascii.GetBytes(inputString);
 9InBlock.gif        for (int i = 0; i < s.Length; i++)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            if ((int)s[i] == 63)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                tempLen += 2;
14ExpandedSubBlockEnd.gif            }

15InBlock.gif            else
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                tempLen += 1;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif
20InBlock.gif            try
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                tempString += inputString.Substring(i, 1);
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            catch
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                break;
27ExpandedSubBlockEnd.gif            }

28InBlock.gif
29InBlock.gif            if (tempLen > len)
30InBlock.gif                break;
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
33InBlock.gif        if (mybyte.Length > len)
34InBlock.gif            tempString += "";
35InBlock.gif
36InBlock.gif        return tempString;
37ExpandedSubBlockEnd.gif    }

38ExpandedBlockEnd.gif    #endregion
Repeat中调用该函数:
ContractedBlock.gif ExpandedBlockStart.gif Code
1None.gif  <asp:Repeater ID="Repeater1" runat="server">
2None.gif              <ItemTemplate>
3None.gif                <a href='<%# "Bulletin/Details.aspx?BulletinID="+ DataBinder.Eval(Container.DataItem,"ID") %>'><%# Warning.CutString(DataBinder.Eval(Container.DataItem, "Title").ToString(),22)%></a>
4None.gif              </ItemTemplate>
5None.gif              </asp:Repeater>

去除最后那个“|”字符

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif    private string ClearLastChar(string str)
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif        if (str == null)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 5InBlock.gif            return str;
 6ExpandedSubBlockEnd.gif        }

 7InBlock.gif        else
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            if (str.Length > 0)
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
11InBlock.gif                return str.Substring(0, str.LastIndexOf("|"));
12ExpandedSubBlockEnd.gif            }

13InBlock.gif            else
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return "";
16ExpandedSubBlockEnd.gif            }

17InBlock.gif
18ExpandedSubBlockEnd.gif        }

19ExpandedBlockEnd.gif    }

20None.gif

转载于:https://www.cnblogs.com/zhangchenliang/archive/2008/01/11/1035703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值