身份证的前6位就是表示开出身份证所在地的区划代码。
图:
类:
Public Class RegionalclassClass Regionalclass
Private _Regionals As RegionalCollection
Private File As String = System.AppDomain.CurrentDomain.BaseDirectory & "\全国行政区划代码.txt"
Private IsOK As Boolean = False
Public ReadOnly Property ItemsCollection()Property ItemsCollection() As RegionalCollection
Get
Return _Regionals
End Get
End Property
Public Sub Load()Sub Load()
IsOK = System.IO.File.Exists(File)
If Not IsOK Then
MsgBox("文件不存在!")
Exit Sub
End If
_Regionals = New RegionalCollection
Dim fs As IO.FileStream = New IO.FileStream(File, IO.FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fs, System.Text.Encoding.Default)
Dim Line As String
Dim tmp As Regional
Dim tmp1 As Regional
Dim tmp2 As Regional
While sr.Peek <> -1
Line = sr.ReadLine
If Line.Length > 6 Then
If Char.IsNumber(Line, 0) AndAlso Char.IsNumber(Line, 5) Then
tmp = New Regional(Line.Substring(0, 6), Line.Substring(6).Trim)
If tmp.Code.Substring(4) = "00" Then '第五、第六位是0
If tmp.Code.Substring(2, 2) = "00" Then '第三、四位是0
'代表省
tmp.FullName = tmp.Name
Else
'其中01-20、51-70表示省直辖市,21-50表示地区(州、盟)
tmp1 = _Regionals.Item(tmp.Code.Substring(0, 2) & "0000")
If Not tmp1 Is Nothing Then
tmp.FullName = tmp1.Name & tmp.Name
End If
End If
Else
'01-18表示市辖区或地辖区,21-80表示县(旗),81-99表示省直辖县级市
tmp1 = _Regionals.Item(tmp.Code.Substring(0, 2) & "0000")
If Not tmp1 Is Nothing Then
If tmp1.Name.Substring(tmp1.Name.Length - 1).Equals("市") Then
tmp.FullName = tmp1.FullName & tmp.Name
Else
tmp2 = _Regionals.Item(tmp.Code.Substring(0, 4) & "00")
If Not tmp2 Is Nothing Then
tmp.FullName = tmp2.FullName & tmp.Name
End If
End If
End If
End If
_Regionals.Add(tmp)
End If
End If
End While
sr.Close()
fs.Close()
End Sub
Public Class RegionalCollectionClass RegionalCollection
Inherits System.Collections.DictionaryBase
Public Sub Add()Sub Add(ByVal iItem As Regional)
Me.Dictionary.Add(iItem.Code, iItem)
End Sub
Public Sub Remove()Sub Remove(ByVal Code As String)
Me.Dictionary.Remove(Code)
End Sub
Default Public ReadOnly Property Item()Property Item(ByVal Code As String) As Regional
Get
Return CType(Me.Dictionary.Item(Code), Regional)
End Get
End Property
Public Function SearchByCode()Function SearchByCode(ByVal iCode As String) As Regional()
If Me.Count = 0 Then Return Nothing
iCode = iCode.Trim
If iCode.Equals(String.Empty) OrElse iCode.Equals("%") Then Return All()
If iCode.Length > 6 Then Return Nothing '多于6位
'由于对正则不熟悉,在此略去对iCode含有通配符%、_合理性的检查
' If.
Dim tmp As Regionalclass.Regional
Dim Result(-1) As Regional
Dim n As Integer = 0
If iCode.IndexOfAny("%_") = -1 Then
' 通过GetEnumerator.MoveNext方法得到的Item并不是按Code排序的,若考虑上排序时间,这个方法估计还快些。没验证。
For i As Integer = 100000 To 999999
tmp = Item(i.ToString)
If Not tmp Is Nothing Then
If tmp.Code.IndexOf(iCode) <> -1 Then
ReDim Preserve Result(n)
Result(n) = tmp
n += 1
End If
End If
Next
Else
If iCode.StartsWith("%") Then
iCode = iCode.Substring(1)
For i As Integer = 100000 To 999999
tmp = Item(i.ToString)
If Not tmp Is Nothing Then
If tmp.Code.EndsWith(iCode) Then
ReDim Preserve Result(n)
Result(n) = tmp
n += 1
End If
End If
Next
End If
If iCode.EndsWith("%") Then
iCode = iCode.Substring(0, iCode.Length - 1)
For i As Integer = 100000 To 999999
tmp = Item(i.ToString)
If Not tmp Is Nothing Then
If tmp.Code.StartsWith(iCode) Then
ReDim Preserve Result(n)
Result(n) = tmp
n += 1
End If
End If
Next
End If
If iCode.Length = 6 AndAlso iCode.IndexOf("_") <> -1 Then
'这里要学会正则才行,要不枚举太麻烦了
End If
End If
Return Result
End Function
Public Function SearchByName()Function SearchByName(ByVal iName As String) As Regional()
If Me.Count = 0 Then Return Nothing
'iName = iName.Trim
If iName.Equals(String.Empty) OrElse iName.Equals("*") Then Return All()
Dim tmp As Regionalclass.Regional
Dim Result(-1) As Regional
Dim n As Integer = 0
Dim tmpStr As String
For i As Integer = 100000 To 999999
tmp = Item(i.ToString)
If Not tmp Is Nothing Then
tmpStr = tmp.Name
tmpStr = tmpStr.Replace(" ", "")
If tmpStr.IndexOf(iName) <> -1 Then
ReDim Preserve Result(n)
Result(n) = tmp
n += 1
End If
End If
Next
Return Result
End Function
Public Function All()Function All() As Regional()
If Me.Count = 0 Then Return Nothing
Dim tmp As Regionalclass.Regional
Dim Result(Me.Count - 1) As Regional
Dim n As Integer = 0
For i As Integer = 100000 To 999999
tmp = Item(i.ToString)
If Not tmp Is Nothing Then
Result(n) = tmp
n += 1
End If
Next
Return Result
End Function
End Class
Public Class RegionalClass Regional
Private _Code As String
Private _Name As String
Private _FullName As String
Sub New()Sub New()
End Sub
Sub New()Sub New(ByVal iCode As String, ByVal iName As String)
_Code = iCode
_Name = iName
End Sub
Public Property Code()Property Code() As String
Get
Return _Code
End Get
Set(ByVal Value As String)
_Code = Value
End Set
End Property
Public Property Name()Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property FullName()Property FullName() As String
Get
Return _FullName
End Get
Set(ByVal Value As String)
_FullName = Value
End Set
End Property
Public Function ToArray()Function ToArray() As Array
Return New String() {_Code, _Name, _FullName}
End Function
Public Overrides Function ToString()Function ToString() As String
Return _Code & " " & _FullName
End Function
End Class
End Class
测试代码:
Public Class Form1Class Form1
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
Public Sub New()Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
Friend WithEvents Button4 As System.Windows.Forms.Button
Private Sub InitializeComponent()Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Button3 = New System.Windows.Forms.Button
Me.ListView1 = New System.Windows.Forms.ListView
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
Me.Button4 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Button1.Location = New System.Drawing.Point(16, 240)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button2.Location = New System.Drawing.Point(248, 240)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(112, 32)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
'
'TextBox1
'
Me.TextBox1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox1.Location = New System.Drawing.Point(248, 168)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(112, 21)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox2.Location = New System.Drawing.Point(448, 168)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(120, 21)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'TextBox3
'
Me.TextBox3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox3.Location = New System.Drawing.Point(248, 200)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(320, 21)
Me.TextBox3.TabIndex = 4
Me.TextBox3.Text = "TextBox3"
'
'Button3
'
Me.Button3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button3.Location = New System.Drawing.Point(456, 240)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(112, 32)
Me.Button3.TabIndex = 5
Me.Button3.Text = "Button3"
'
'ListView1
'
Me.ListView1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3})
Me.ListView1.GridLines = True
Me.ListView1.Location = New System.Drawing.Point(16, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(544, 136)
Me.ListView1.TabIndex = 6
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ColumnHeader1
'
Me.ColumnHeader1.Text = "区划码"
Me.ColumnHeader1.Width = 88
'
'ColumnHeader2
'
Me.ColumnHeader2.Text = "名称"
Me.ColumnHeader2.Width = 112
'
'ColumnHeader3
'
Me.ColumnHeader3.Text = "全名"
Me.ColumnHeader3.Width = 326
'
'Button4
'
Me.Button4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Button4.Location = New System.Drawing.Point(16, 168)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(104, 32)
Me.Button4.TabIndex = 7
Me.Button4.Text = "Button4"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(576, 285)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim t As New Regionalclass
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t.Load()
End Sub
Private Sub Button4_Click()Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ShowItems(t.ItemsCollection.All)
End Sub
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ShowItems(t.ItemsCollection.SearchByCode(Me.TextBox1.Text))
End Sub
Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ShowItems(t.ItemsCollection.SearchByName(Me.TextBox2.Text))
End Sub
Private Sub ShowMsg()Sub ShowMsg(ByVal m As Regionalclass.Regional)
If m Is Nothing Then
With Me
.TextBox1.Clear()
.TextBox2.Clear()
.TextBox3.Clear()
End With
Else
With Me
.TextBox1.Text = m.Code
.TextBox2.Text = m.Name
.TextBox3.Text = m.FullName
End With
End If
End Sub
Private Sub ShowItems()Sub ShowItems(ByVal oItems As Regionalclass.Regional())
Me.ListView1.BeginUpdate()
Me.ListView1.Items.Clear()
Me.ListView1.EndUpdate()
If oItems Is Nothing OrElse oItems.Length = 0 Then Exit Sub
Me.ListView1.BeginUpdate()
Dim tmp As Regionalclass.Regional
For Each tmp In oItems
Me.ListView1.Items.Add(New ListViewItem(tmp.ToArray))
Next
Me.ListView1.Items.Add(New ListViewItem(New String() {"记录总数", ListView1.Items.Count.ToString}))
Me.ListView1.EndUpdate()
Me.TextBox3.Text = tmp.ToString
End Sub
End Class
最新数据存在程序路径下的 “全国行政区划代码.txt”文件,以下为文件内容:
说明:
代码由六位数字组成,每两位数字一组共三组,即XX XX XX,意见如下:
首组,代表省
中组,其中01-20、51-70表示省直辖市,21-50表示地区(州、盟)
尾组,01-18表示市辖区或地辖区,21-80表示县(旗),81-99表示省直辖县级市
-----------------------------------------
-------- 国家统计局设管司 2005-08-01 15:56:18------
------------ 最新数据截止2005年6月30日-------
------------ 数据更新网址: http://www.stats.gov.cn/tjbz/xzqhdm/index.htm---
---------- LzmTW 2005年10月22日-------
------------------------------------------------
代码 名称
110000 北京市
110100 市辖区
110101 东城区
110102 西城区
110103 崇文区
110104 宣武区
110105 朝阳区
110106 丰台区
110107 石景山区
110108 海淀区
110109 门头沟区
110111 房山区
110112 通州区
110113 顺义区
110114 昌平区
110115 大兴区
110116 怀柔区
110117 平谷区
110200 县
110228 密云县
110229 延庆县
完整代码请参考:http://lzmtw.cnblogs.com/archive/2005/10/22/260066.html
本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow