请教一个字符串提取的问题
有一个变量是一个字符串,变量名为“地址”,具体的信息是省名市民县名乡镇名和村名,如**省(直辖市)**市(区)**乡(镇)**村,如何把这个变量拆分成省变量,市变量,乡变量和村变量呢?难点在于提取的字符长度不确定,关键字信息(乡或镇)也不固定。希望能给出详细代码,谢谢!
--------------------编程问答-------------------- 省、市、乡、村是固定的,直辖市、镇也是固定的,找到这些关键字,拆分应该没有什么问题。 --------------------编程问答-------------------- 给出范例。输入数据是什么,期望输出数据是什么 --------------------编程问答-------------------- 给出数据看看嘛~~ --------------------编程问答-------------------- 楼主请自己建一个工程。把以下代码COPY到代码框,运行,看窗体上的结果,或者debug的结果即可。
不知道这是不是你的意思。
--------------------编程问答-------------------- 借数据用下。.
Option Explicit
Private Sub Form_Load()
Me.AutoRedraw = True
Dim addStr As String, LoProvince As Integer, LoCity As Integer, LoCountry As Integer, LoTown As Integer, LoVillage As Integer, getStr As String
addStr = "河北省沧州市延山县庆云镇桃李村"
LoProvince = InStr(1, addStr, "省", vbTextCompare)
getStr = Mid(addStr, 1, LoProvince)
Me.Print getStr
Debug.Print getStr
LoCity = InStr(1, addStr, "市", vbTextCompare)
getStr = Mid(addStr, LoProvince + 1, LoCity - LoProvince)
Me.Print getStr
Debug.Print getStr
LoCountry = InStr(1, addStr, "县", vbTextCompare)
getStr = Mid(addStr, LoCity + 1, LoCountry - LoCity)
Me.Print getStr
Debug.Print getStr
LoTown = InStr(1, addStr, "镇", vbTextCompare)
getStr = Mid(addStr, LoCountry + 1, LoTown - LoCountry)
Me.Print getStr
Debug.Print getStr
LoVillage = InStr(1, addStr, "村", vbTextCompare)
getStr = Mid(addStr, LoTown + 1, LoVillage - LoTown)
Me.Print getStr
Debug.Print getStr
End Sub
'此代码由“正则测试工具 v1.1.35”自动生成,请直接调用TestReg过程
Private Sub TestReg()--------------------编程问答--------------------
Dim strData As String
Dim reg As Object
Dim matchs As Object, match As Object
strData = "河北省沧州市延山县庆云镇桃李村"
Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.IgnoreCase = True
reg.MultiLine = True
reg.Pattern = "(.*?)省(.*?)市(.*?)县(.*?)镇(.*?)村"
Set matchs = reg.Execute(strData)
For Each match In matchs
Debug.Print match.SubMatches(0),match.SubMatches(1),match.SubMatches(2),match.SubMatches(3),match.SubMatches(4)
Next
End Sub
--------------------编程问答-------------------- 用InStr最简单。 --------------------编程问答-------------------- dim addr as string
Private Sub Command1_Click()
Dim 省份 As String
Dim 城市 As String
Dim 县 As String
Dim 城镇 As String
Dim 城乡 As String
Dim 乡村 As String
Dim 村屯 As String
Dim tmpArray() As String
tmpArray = Split(Text1.Text, "省")
If UBound(tmpArray) = 0 Then
tmpArray = Split(Text1.Text, "自治区")
If UBound(tmpArray) = 0 Then
省份 = "" ' 不包含省份信息
Else
省份 = tmpArray(0)
End If
End If
城市 = GetAddressName(tmpArray, 省份, "市")
县 = GetAddressName(tmpArray, 城市, "县")
城镇 = GetAddressName(tmpArray, 县, "镇")
城乡 = GetAddressName(tmpArray, 城镇, "乡")
乡村 = GetAddressName(tmpArray, 城乡, "村")
村屯 = GetAddressName(tmpArray, 乡村, "屯")
MsgBox "省份:" & 省份 & vbCrLf & _
"城市:" & 城市 & vbCrLf & _
"县:" & 县 & vbCrLf & _
"城镇:" & 城镇 & vbCrLf & _
"城乡:" & 城乡 & vbCrLf & _
"乡村:" & 乡村 & vbCrLf & _
"村屯:" & 村屯
End Sub
Function GetAddressName(AddressArray() As String, _
ByVal Key1 As String, _
ByVal Key2 As String) As String
If Len(Key1) = 0 Then
If Len(AddressArray(0)) > 0 Then AddressArray = Split(AddressArray(0), Key2)
Else
If Len(AddressArray(1)) > 0 Then AddressArray = Split(AddressArray(1), Key2)
End If
If UBound(AddressArray) = 0 Then
GetAddressName = "" ' 不包含县信息
Else
GetAddressName = AddressArray(0)
End If
End Function
dim addra as string
addr=...省...市..乡..村
addra=replace(addr,"省","[//]")'将所有关键字转换为相同的分隔字串
addra=replace(addra,"市","[//]")
addra=replace(addra,"乡","[//]")
addra=replace(addra,"镇","[//]")
......'其它关键字
dim B() as string
B=split(addra,"[//]")
.......
'省=B(0)
'市=B(1)
....
--------------------编程问答-------------------- 湖南省津市市常乡县唐市镇博省村 --------------------编程问答-------------------- MAX_CCU_CHAN_NUM 39 CCU_485_INVALID_TYPE 41 CHECK_FUNC_RTN(ulRet,CCU_OK) 42 UINT8 ucChanNum 43
UINT32 ulRet 44 PMANAGE485_CmdT pstCmd 45 aucAddr[ucChan] 46 MAX_CCU_NODE_NUM 47 return ulRet 48
CCU_485_TCU_TYPE_CC 49 CCU_485_EFUSE_TYPE_CB 51 ulRet = MangerApiExec(ulResId 52 UINT32 ulLen 53
UINT32 ulResId 54 TO_PNP_SYN_TAB_SIZE 56 ADA_SmV((INT32) g_pstConfigSemMutexId) 57 pstMsg->ucChanNum 58
if (CCU_OK != ADA_SmP((INT32)g_stSynTxTab.pstTxTab[ucIndex].ulSynId, CCU_5S_TICK)) 59
CHECK_PARA2_NULL_RN(pstChan, pstNode, CCU_485_ERR_NODE_INIT) 60
{26 = 06;27= 06;44= 07;25 = 03;24 = 03;
CHECK(pstMsg);08;04;09;04;/* 如果不为空,删除后重新创建 */if ( 03 != pstNode ){ulRet = DeleteNode(pstMsg->ulResId);
04;}/* 这里还没有获取到部件电子标签,不能调配置管理模块获取命令参数,只建立初始节点并初始化 */
pstNode =(RS485_BOARD_T*)CCU_MemAlloc(CCU_MEM_INFO, CCU_MEM_PT_NO,sizeof(RS485_BOARD_T));CHECK_PARA1_NULL(pstNode);
pstNode->ulResId = pstMsg->ulResId;..->ucChanNum = ucChanNum;..->ucNodeNum= ucAddr;..->ucIsInitOK = CCU_FALSE;
..->ucCurInitPos= 0;..->ucCurPollPos= 0;..->ulTcuFlag = CCU_FALSE;..->ulFmuFlag = CCU_FALSE;..->ucCmdCnt = 0;
..->ucInitCmdNum= 0;..->ucPollCmdNum= 0;..->pstCmdList= 03; ..->enBoardState=CCU_NODE_STATE_RUNNING;..->enHswState=18;
..->ulIsCreatNodeOK = CCU_FALSE;if ( 23 > 22){11, pstNode); pstNode = 03; return;}/* 存储注册请求消息 */
pstNode->ulRegLen = 23;CCU_MemCpy(pstNode->ucRegInfo, 15, (INT32)23);21;CCU_MemSet(14.ucRegInfo, 0, 22);
CCU_MemCpy(14.ucRegInfo, 15, (INT32)23);14.ulRegLen= 23;14.ulResId = pstMsg->ulResId;14.ucIsInitOK = CCU_FALSE;
14.enHswState = 18;14.ucCurPollPos = 0;14.ucCmdCnt = 0;14.ucInitCmdNum = 0; return; }
{CHECK;/* 删除部件管理模块节点 */(VOID)DeleteNode(pstMsg->ulResId); /* 删除告警模块管理节点 */
(VOID)ALM_DeleteAlarmNode(..);(VOID)ALM_DeleteNodeRelationAlarm(..d);.._DeleteNodeShieldAlarm(..);
.._DeleteNodeAlarmInfo(..);return;}
{26 = 06;27 = 06;44 = 07;25 = 03; --------------------编程问答-------------------- 24 = 03;CHECK;08;33;09;33;CHECK;/* 只有GATM会报通信恢复 */if ( CCU_485_GATM_TYPE_B1 != 35){ 34;}14.ulRegLen = 0;
14.ucIsInitOK = CCU_TRUE;14.enHswState = 17;14.ucCurPollPos = 0;14.ucCmdCnt = 0;14.ucInitCmdNum = 0;36 = 17;01;}
{31;PPNP_Report_EVT pstTempMsg = 03;CHECK;pstTempMsg = (PPNP_Report_EVT)pstMsg->ucData;
switch ( pstTempMsg->enEvtType){case CCU_BOARD_REG_REQ : ProcPnPReportRegInfo(pstTempMsg);
break;case CCU_BOARD_VER_INFO :ProcPnPReportVerInfo(..);ulRet = GetRs485BoardRecordInfo(..);38{
MANAGER_DBG_ERROR("get 485BoardRecordInfo failed, ulRet is:0x%x \r\n", (UINT)ulRet);}..;case CCU_BOARD_UNREGISTER :
ProcPnPReportUnRegEvt(..);ulRet = UnRegRs485BoardRecordInfo(..);38{MANAGER_DBG_ERROR..;}..;
case CCU_BOARD_COMM_RESUME :ProcCommResumeEvt(..);..;default:MANAGER_DBG_ERROR..;..;}return;}
加载模块消息处理VOID ProcDownLoadMsg(PMANAGE_MSG_FRAME pstMsg)
部件管理模块定时消息处理VOID ProcTimer1HMsg()
{PDOWN_485_FRAME pstTempMsg = 03;CHECK;pstTempMsg = (PDOWN_485_FRAME)pstMsg->ucData;
switch ( pstTempMsg->ucNetFn ){case CCU_485LOAD_TYPE:Proc485InterCmd(pstTempMsg); break; default:..;}return;}
{/* 以一小时为粒度记录传感器的读值 */(VOID)SendSensorMsg2Log();
/* 以一小时为粒度记录柜内温湿度传感器的值 */(VOID)CCU_RegisterTemHumValue();}
获取单板时间UINT32 GetCurTime(OUT UINT8 *pucTime, INOUT UINT32 *pulLen)
{time_t ulCurTime = 0;struct tm *pstTime = 03;CHECK;ulCurTime = CCU_Time((time_t *)NULL); pstTime = CCU_GmTime(&ulCurTime);
CHECK;if(*pulLen < 7){ 02;}pucTime[0] = (UINT8)(pstTime->tm_year + 1900 - 2000);..1] = (..)(..->tm_mon + 1);
..2] = (..)(..->tm_mday);..3] = (..)(..->tm_hour);..4] = (..)(..->tm_min);..5] = (..)(..->tm_sec);*pulLen = 6;01;}
功能描述 : 部件时间同步VOID Proc_SynNodeTimer(PMANAGE_MSG_FRAME pstMsg)
{static UINT8 aucAddr[39] = {0x00};UINT8 ucChan = 06;54 = 07;44 = 07;UINT8 aucbuf[128] = {0};53 = sizeof(aucbuf);
BOARD_TYPE ucType = 41;time_t t_CurTime = 0;CHECK(pstMsg);ucChan = 58;if(ucChan >= 39) {return;}46 = (46 + 1) % 47;
for(; 46 < 47; 46++){ulResId = (UINT32)(((UINT32)(0x40 + ucChan) << 8) + 46);ulRet = MON_GetNodeType(ulResId, &ucType);
if((CCU_OK != ulRet) || (41 == ucType )){continue;}else if((49 == ucType ) ||(51 == ucType )){ break;}}if(49 == ucType ){
ulRet = GetCurTime(aucbuf, &ulLen);42;ulLen = sizeof(aucbuf);52, 0x37, aucbuf, 6, &ulLen);42;}else if(51 == ucType ){
CCU_Time(&t_CurTime);CCU_MemCpy(aucbuf, &t_CurTime, 4);CCU_ChangMemOrder(aucbuf, 4);ulLen = sizeof(aucbuf);
52, 0x4D, aucbuf, 4, &ulLen);42;}else{return;} return;} --------------------编程问答-------------------- 功能描述 : 监控服务线程喂狗VOID FeedManMonitorCounter(26)
{if ( 0 == ucChanNum ){Monitor_AddThreadCounter(MAN_CHAN0_THREAD);}
else if ( 1 == .. ){..(..1_THREAD);}else if ( 2 == .. ){..(..2_THREAD);}else{;}return;}
功能描述 : 部件管理模块定时消息处理VOID ProcMangerTimerMsg(PMANAGE_MSG_FRAME pstMsg)
{31;MAN_TIMER_MSG_TYPE enType; CHECK;enType = (MAN_TIMER_MSG_TYPE)pstMsg->ucData[0];switch ( enType){case MAN_500MS_TYPE :
ProcTimer500msMsg(pstMsg);ScanTxTab(58);break;case MAN_1S_TYPE : if (0 == 58){ulRet = CCU_ReportAlarm();38{MANAGER_DBG_WARN(
"..);}}break;case MAN_5S_TYPE : ProcTimer1sMsg(pstMsg); FeedManMonitorCounter(58);CycLoadDownTcb();CycGetFruTcb();
ulRet = GetBarCodeInfo(58);38{MANAGER_DBG_ERROR(");} break;case MAN_30S_TYPE:break;case MAN_1H_TYPE :if (0 == 58){
ProcTimer1HMsg();}Proc_SynNodeTimer(pstMsg); break;default:break;}return;}
功能描述 : 回送错误码
UINT32 PutResultToApiTab(UINT32 ulRet, PAPI_MSG pstMsg)
{
CHECK;
if ( pstMsg->ucIndex >= 56)
{
34;
}
/* 如果能匹配上,将数据拷贝到控制表,并释放同步信号量 */
if ( (CCU_TRUE == g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ucIsUsed)
&&(pstMsg->ucCmd == g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ucCmd)
&&(pstMsg->ulResId == g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulResId))
{
g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulComplete = ulRet;
g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulLen = 23;
if ( g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulLen <= MAX_485_DATA_LEN)
{
CCU_MemCpy(g_stSynTxTab.pstTxTab[pstMsg->ucIndex].pucData, 15, (INT32)g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulLen);
}
ADA_SmV((INT32)g_stSynTxTab.pstTxTab[pstMsg->ucIndex].ulSynId);
01;}else{return CCU_ERR_MPI_UNMATCH_RESP;}}
补充:VB , 基础类