文字输入转换

来源:互联网 发布:2016人口老龄化数据 编辑:程序博客网 时间:2024/04/30 14:21

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Text;
using System.IO;

namespace changeinput1
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class changeinput
 {
  public changeinput()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //

  }
  public string zhuanhuan(string zs)
  {
   Encoding asc = Encoding.ASCII;
   bool tag=false;
   byte[] b=new byte[]{};
   string s=kongge1(zs);
   StringBuilder sb=new StringBuilder(s);
   char[] ch=new char[]{};
   ch=s.ToCharArray();   
   b=asc.GetBytes(ch,0,ch.Length);
   int tempi=0;
   int start=0;
   int length=0;
   string temps="";
   string temps1="";
   string temps2="";
   string[] result=new string[]{}; 
   for(int i=0;i<=ch.Length-1;i++)
   {    
    if((b[i]==10))//当是"/n"时处理
    {
     tag=true;
     if(b[i-2]==10)
     {
      temps1="<BR>"; 
      temps2=temps2+temps1;
     }
     else
     {
      tempi=i;     
      length=i-start-1;   
      temps=s.Substring(start,length);     
      temps1=kongge(temps)+"<BR>";//处理空格 
      temps2=temps2+temps1;
      start=tempi+1;
     }
     //     Page.Response.Write(temps1);
     //     Page.Response.Write("<BR>");
     temps1="";
    }  
   }
   if(!tag)//当字符中没有换行,既只有一行
   {
    temps2=kongge(s);
    //    Page.Response.Write(temps1);
    //    Page.Response.Write("<BR>");     
   }
   return temps2;
  }

  public string kongge1(string s)//处理文字中的"/t"
  {
   string re="";
   int tempi=0;
   int start=0;
   int length=0;
   string temps="";
   Encoding asc = Encoding.ASCII;
   byte[] b=new byte[]{};
   char[] ch=new char[]{};
   char[] ch1=new char[]{};
   string k="        ";
   ch=s.ToCharArray();   
   b=asc.GetBytes(ch,0,ch.Length);
   if(b[b.Length-1]!=10)//如果最后一个字符不是/n
   {
    s=s+"/r/n";
   }
   ch=s.ToCharArray(); 
   b=asc.GetBytes(ch,0,ch.Length);
   for(int i=0;i<=b.Length-1;i++)
   {    
    if(b[i]==9)
    {
     re=re+k;
     start=i+1;
    }
    else
    {
     length=1;   
     temps=s.Substring(start,length);
     tempi=i;
     start=tempi+1;     
     re=re+temps;
    }
   }   
   return re;
  }
  public string kongge(string s)//处理空格
  {
   string temps1="";
   for(int k=0;k<=s.Length-1;k++)
   {
       
    if(k==s.Length-1)//最后一个字符
    {
     string s1=s.Substring(k);
     if(String.Compare(s1," ")==0)
     {
      temps1=temps1+"&nbsp";
     }
     else
      temps1=temps1+s1;
    }
    else
    {
     string s2=s.Substring(k, 1);
     if(String.Compare(s2," ")==0)
     {
      temps1=temps1+"&nbsp";
     }
     else
      temps1=temps1+s2;
    }      

   }
   return temps1;

  }
 }
}

原创粉丝点击