白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks

 

using System;
using System.Collections.Generic;
using System.Text;

namespace NcZerg
{
    
/// <summary>
    
/// 字符串操作函数
    
/// </summary>

    public class StringHandle
    
{
        
/// <summary>
        
/// 截取字符串并且关键字颜色显示
        
/// </summary>
        
/// <param name="StringValue">输入字符串</param>
        
/// <param name="HighValues">关键字</param>
        
/// <param name="Len">显示长度</param>
        
/// <returns></returns>

        public string Highline(String StringValue, String[] HighValues, int Len)
        
{
            
if (StringValue == String.Empty) return String.Empty;
            List
<int> BeforePositions = new List<int>();
            List
<int> AfterPositions = new List<int>();
            String BeforeStr 
= "<font color=\"red\">";
            String AfterStr 
= "</font>";
            String StringPad 
= "";
            String[] SplitValues;
            
int before, after;
            
for (int i = 0; i < HighValues.Length; i++)
            
{
                
if (HighValues[i] == String.Empty) continue;
                SplitValues 
= StringValue.Split(new string[] { HighValues[i] }, StringSplitOptions.None);
                
if (SplitValues.Length == 1continue;
                
for (int j = 1; j < SplitValues.Length; j++)
                
{
                    before 
= after = 0;
                    
for (int k = 0; k < j; k++)
                    
{
                        before 
+= SplitValues[k].Length;
                        after 
+= SplitValues[k].Length;
                    }

                    before 
+= (j - 1* HighValues[i].Length;
                    after 
+= j * HighValues[i].Length;

                    BeforePositions.Add(before);
                    AfterPositions.Add(after);
                }

            }


            BeforePositions.Sort();
            AfterPositions.Sort();
            StringValue 
= InterceptString(StringValue, Len);
            
int AddLen = 0;
            
for (int i = 0; i < BeforePositions.Count; i++)
            
{
                
if (i > 0 && BeforePositions[i] < AfterPositions[i - 1])
                
{
                    
continue;
                }


                
if ((StringValue.EndsWith(StringPad) && StringValue.Length - 3 >= BeforePositions[i] + AddLen)
                    
|| (!StringValue.EndsWith(StringPad) && StringValue.Length >= BeforePositions[i] + AddLen))
                
{
                    StringValue 
= StringValue.Insert(BeforePositions[i] + AddLen, BeforeStr);
                    AddLen 
+= BeforeStr.Length;

                    
if ((StringValue.EndsWith(StringPad) && StringValue.Length - 3 >= AfterPositions[i] + AddLen)
                    
|| (!StringValue.EndsWith(StringPad) && StringValue.Length >= AfterPositions[i] + AddLen))
                    
{
                        StringValue 
= StringValue.Insert(AfterPositions[i] + AddLen, AfterStr);
                        AddLen 
+= AfterStr.Length;
                    }

                    
else
                    
{
                        StringValue 
+= AfterStr;
                        
break;
                    }

                }

            }

            
if (StringValue.LastIndexOf(BeforeStr) > StringValue.LastIndexOf(AfterStr))
            
{
                StringValue 
+= AfterStr;
            }

            
return StringValue;
        }


        
/// <summary>
        
/// 截取字符串函数
        
/// </summary>
        
/// <param name="Str">输入字符串</param>
        
/// <param name="Len">截取长度</param>
        
/// <returns></returns>

        private String InterceptString(String StringValue, int Len)
        
{
            String Res 
= String.Empty;
            String StringPad 
= "";
            
int Sum = 0;
            
if (getStringLen(StringValue) <= Len)
            
{
                
return StringValue;
            }

            
else
            
{///截取左边 Str 第 Len - 3 个字符串返回
                for (int i = 0; i < StringValue.Length; i++)
                
{
                    
if (Sum >= Len - 3)
                    
{
                        
break;
                    }

                    
if (((int)(StringValue[i])) > 126)
                    
///是汉字 Sum += 2
                        Sum += 2;
                        
if (Sum > Len - 3)
                        
{
                            
break;
                        }

                    }

                    
else
                    
{
                        Sum
++;
                    }


                    Res 
+= StringValue[i].ToString();
                }


                Res 
+= StringPad;
            }


            
return Res;
        }


        
/// <summary>
        
/// 获取字符串长度函数
        
/// </summary>
        
/// <param name="Str"></param>
        
/// <returns></returns>

        public int getStringLen(String StringValue)
        
{
            
int sum = 0;
            
foreach (char c in StringValue)
            
{
                
if (((int)(c)) > 126)
                
{
                    sum
++;
                }

                sum
++;
            }

            
return sum;
        }


    }

}


posted on 2007-12-20 12:08 白开心 阅读(362) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。