汉字转拼音缩写的函数以及其他函数

该博客提供了多个实用函数。包括清空页面指定控件内容,汉字转拼音缩写,ListBox与字符串相互转换,ListBox间数据移动,获取月份天数集合以及选中天数的最小和最大天数等,调用时ListItemCollection可用ListBox.Items作参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 清空指定页面上所有的控件内容,包括TextBox,CheckBox,CheckBoxList,RadioButton,RadioButtonList。但是不清
InBlock.gif        
/// 除如ListBox,DropDownList,因为这样的控件值对当前页面来说还可以用,一般这些控件里都是保存的字典数据。
InBlock.gif        
/// Author:Kevin
InBlock.gif        
/// 日期:2004-12-02
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="page"> 指定的页面</param> 

None.gif        public static void ClearAllContent(System.Web.UI.Control page)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
int nPageControls = page.Controls.Count;
InBlock.gif            
for (int i = 0; i < nPageControls; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (System.Web.UI.Control control in page.Controls[i].Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (control.HasControls())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ClearAllContent(control); 
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        
if (control is TextBox)
InBlock.gif                            (control 
as TextBox).Text = "";
InBlock.gif
InBlock.gif                        
if (control is CheckBox)
InBlock.gif                            (control 
as CheckBox).Checked = false;
InBlock.gif
InBlock.gif                        
if (control is RadioButtonList)
InBlock.gif                            (control 
as RadioButtonList).SelectedIndex = -1;
InBlock.gif
InBlock.gif                        
if (control is RadioButton)
InBlock.gif                            (control 
as RadioButton).Checked = false;
InBlock.gif
InBlock.gif                        
if (control is CheckBoxList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
foreach (ListItem item in (control as CheckBoxList).Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                item.Selected 
= false;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }
//if..else
ExpandedSubBlockEnd.gif
                }
//foreach
ExpandedSubBlockEnd.gif
            }
//for
ExpandedBlockEnd.gif
        }
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 汉字转拼音缩写
InBlock.gif        
/// Code By MuseStudio@hotmail.com
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="str">要转换的汉字字符串</param>
ExpandedBlockEnd.gif        
/// <returns>拼音缩写</returns>

None.gif        public string GetPYString(string str)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
string tempStr = "";
InBlock.gif            
foreach(char c in str)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if((int)c >= 33 && (int)c <=126)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//字母和符号原样保留
InBlock.gif
                    tempStr += c.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//累加拼音声母
InBlock.gif
                    tempStr += GetPYChar(c.ToString());
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return tempStr;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// 取单个字符的拼音声母
InBlock.gif        
/// Code By MuseStudio@hotmail.com
InBlock.gif        
/// 2004-11-30
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="c">要转换的单个汉字</param>
ExpandedBlockEnd.gif        
/// <returns>拼音声母</returns>

None.gif        public string GetPYChar(string c)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
byte[] array = new byte[2];
InBlock.gif            array 
= System.Text.Encoding.Default.GetBytes(c);
InBlock.gif            
int i = (short)(array[0- '\0'* 256 + ((short)(array[1- '\0'));
InBlock.gif
InBlock.gif            
if ( i < 0xB0A1return "*";
InBlock.gif            
if ( i < 0xB0C5return "a";
InBlock.gif            
if ( i < 0xB2C1return "b";
InBlock.gif            
if ( i < 0xB4EEreturn "c";
InBlock.gif            
if ( i < 0xB6EAreturn "d";
InBlock.gif            
if ( i < 0xB7A2return "e";
InBlock.gif            
if ( i < 0xB8C1return "f";
InBlock.gif            
if ( i < 0xB9FEreturn "g";
InBlock.gif            
if ( i < 0xBBF7return "h";
InBlock.gif            
if ( i < 0xBFA6return "g";
InBlock.gif            
if ( i < 0xC0ACreturn "k";
InBlock.gif            
if ( i < 0xC2E8return "l";
InBlock.gif            
if ( i < 0xC4C3return "m";
InBlock.gif            
if ( i < 0xC5B6return "n";
InBlock.gif            
if ( i < 0xC5BEreturn "o";
InBlock.gif            
if ( i < 0xC6DAreturn "p";
InBlock.gif            
if ( i < 0xC8BBreturn "q";
InBlock.gif            
if ( i < 0xC8F6return "r";
InBlock.gif            
if ( i < 0xCBFAreturn "s";
InBlock.gif            
if ( i < 0xCDDAreturn "t";
InBlock.gif            
if ( i < 0xCEF4return "w";
InBlock.gif            
if ( i < 0xD1B9return "x";
InBlock.gif            
if ( i < 0xD4D1return "y";
InBlock.gif            
if ( i < 0xD7FAreturn "z";
InBlock.gif
InBlock.gif            
return "*";
ExpandedBlockEnd.gif        }
None.gif//作用:把ListBox中的全部内容转换成一个字符串,各个字段间用,分隔
None.gif  
//
None.gif  
//参数:Lists,需要转换的ListBox.items
None.gif  
//
None.gif  
//返回值:转换好的字符串
None.gif  
//
None.gif
  public string ListToString(ListItemCollection Lists)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   
string result="";
InBlock.gif   
for(int i=0;i<Lists.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (i==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     result
=Lists[i].Text;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     result
=result+","+Lists[i].Text;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
return result;
ExpandedBlockEnd.gif  }
 
None.gif
None.gif  
//
None.gif  
//作用:把string中的全部内容转换成ListItemCollection从而绑定到Listbox
None.gif  
//
None.gif  
//参数:str,需要转换的字符串
None.gif  
//
None.gif  
//返回值:转换好的ListItemCollection
None.gif  
//
None.gif
  public ListItemCollection StringToList(string str)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   ListItemCollection lists
=new ListItemCollection();
InBlock.gif   
if(str=="")                                        //字符串为空
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    errPosition
="ListItemCollection";
InBlock.gif    errMsg
="字符串为空";
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else if(str.IndexOf(",")==0)                        //首位为","
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    errPosition
="ListItemCollection";
InBlock.gif    errMsg
="首位为,";
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else if(str.Substring(str.Length-1,1)==",")        //尾位为","
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    errPosition
="ListItemCollection";
InBlock.gif    errMsg
="尾位为,";
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
while (str.IndexOf(",")>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
int position=str.IndexOf(",") ;
InBlock.gif     lists.Add(str.Substring(
0,position));
InBlock.gif     str
=str.Remove(0,position+1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    lists.Add(str);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return lists;
ExpandedBlockEnd.gif  }

None.gif
None.gif  
//
None.gif  
//作用:把源ListBox中的选中数据移动到目标ListBox
None.gif  
//
None.gif  
//参数:FromLists,源ListBox
None.gif  
//
None.gif
  public static void MoveListBoxSelectedItem
None.gif   (ListItemCollection FromLists,ListItemCollection ToLists)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   
for(int i=FromLists.Count-1;i>=0;i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (FromLists[i].Selected)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     FromLists[i].Selected
=false;
InBlock.gif     ToLists.Add(FromLists[i]);
InBlock.gif     FromLists.Remove(FromLists[i]);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
//
None.gif  
//作用:把源ListBox中的全部数据移动到目标ListBox
None.gif  
//
None.gif  
//参数:FromLists,源ListBox
None.gif  
//
None.gif
  public static void MoveListBoxAllItem
None.gif   (ListItemCollection FromLists,ListItemCollection ToLists)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   
for(int i=FromLists.Count-1;i>=0;i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    FromLists[i].Selected
=false;
InBlock.gif    ToLists.Add(FromLists[i]);
InBlock.gif    FromLists.Remove(FromLists[i]);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
//
None.gif  
//作用:输入年月返回月份的天数的集合
None.gif  
//
None.gif  
//参数:YYYY年,MM月
None.gif  
//
None.gif  
//返回值:本月的天数的ArrayList
None.gif  
//
None.gif
  public static ArrayList GetDaysInMonth(int YYYY,int MM)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   
int day=DateTime.DaysInMonth(YYYY,MM);
InBlock.gif   ArrayList days
=new ArrayList();
InBlock.gif   
for (int i=1;i<=day;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    days.Add(i);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return days;
ExpandedBlockEnd.gif  }

None.gif
None.gif
None.gif  
//
None.gif  
//作用:输入选中天数的集合,返回其中的最小和最大的天数
None.gif  
//
None.gif  
//参数:dates,把Calendar.SelectedDates传入即可
None.gif  
//
None.gif  
//返回值:两个数值的ArrayList,第一个为最小天数,第二个为最大天数
None.gif  
//
None.gif
  public static ArrayList GetMinMaxDate(SelectedDatesCollection dates)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   ArrayList Result
=new ArrayList();
InBlock.gif   DateTime min
=new DateTime();
InBlock.gif   DateTime max
=new DateTime();
InBlock.gif   
for(int i=0;i<dates.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (i>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(dates[i]<min)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      min
=dates[i];
ExpandedSubBlockEnd.gif     }

InBlock.gif     
if(dates[i]>max)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      max
=dates[i];
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     min
=dates[i];
InBlock.gif     max
=dates[i];
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   Result.Add(min);
InBlock.gif   Result.Add(max);
InBlock.gif   
return Result;
ExpandedBlockEnd.gif  }

None.gif
None.gif调用函数是碰到ListItemCollection 使用ListBox.Items做参数
None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值