读取中文字符串长度 AND 按长度截取中文字符串中字符

public int Length(string strLen)
{
    
int l,t,c;
    
int i;
    l
=strLen.Length ;
    t
=l;
    
for( i=0;i<l;i++)
    {
        c
=(int)strLen[i];
        
if( c<0)
        {
            c
=c+65536;
        }
        
if (c>255)
        {
            t
=t+1;
        }
    }
    
return t;
}

public string Substring(string strValue, int startIndex, int length)
{
    
int iStartTemp = 0;
    
int iTemp = 0;
    
string returnString = "";
    
if(Length(strValue) > startIndex)
    {
        
for(int i=0;i<strValue.Length;i++)
        {
            
int c = (int)strValue[i];
            
if( c<0)
                c 
+= 65536;
            
if (c>255)
                iTemp 
+= 2;
            
else
                iTemp 
+= 1;
            
if(iTemp > startIndex)
            {
                iStartTemp 
= i;
                
break;
            }
        }
    }
    
else
        
return returnString;

    iTemp 
= 0;
    
if(Length(strValue) >(startIndex + length))
    {
        
for(int i= iStartTemp;i<strValue.Length;i++)
        {
            
int c = (int)strValue[i];
            
if( c<0)
                c 
+= 65536;
            
if (c>255)
                iTemp 
+= 2;
            
else
                iTemp 
+= 1;
            
if(iTemp > length)
                
break;
            
else
                returnString 
+= strValue[i].ToString();
        }
    }
    
else
    {
        returnString 
= strValue.Substring(iStartTemp);
    }
    
return returnString;
}
posted @ 2006-06-13 11:15  幸福★星  阅读(511)  评论(0编辑  收藏  举报