选择字符串到特定单词的单词,然后删除之后出现的单词

时间:2014-07-16 06:04:58

标签: asp.net vb.net

我有以下格式的文字输入,

联系方式详情

客户名称:

的Eswar

要求:

智能卡印刷机经销商

客户电话:

+ XXXXXXXXXXX

如有任何疑问,请随时致电我们的客户支持部门

在ph.No XXX-XXXXXXXXXX上与客户支持主管联系

我的任务是从中提取客户名称要求:等,并将其存储到数据库中。我正在使用以下代码执行此操作:

dim Customer as string
s = (s.Replace(System.Environment.NewLine, ",").Replace(",,", "")) 'replace enter with         comas to create a single string
content_array = s.Split(",") 'split the string into array  
Customer= find_content("Customer Name :", 16)
Public Function find_content(ByVal sub_str As String, ByVal position As Integer) As String
   Dim result As String
   result = ""
   For i As Integer = 0 To content_array.Length
     If content_array(i) = "" Then
       content_array(i) = "default value"
     Else
       result = Trim(content_array(i).Substring(content_array(i).IndexOf(sub_str) + position))
     End If
      i = i + 1
   Next
Return result
End Function

现在我的问题是在数组中插入空值会在函数中产生问题,并导致错误。请为执行拆分时避免空值(“”)插入数组提出建议。或如上所述的提取细节的替代方法。或者从字符串中提取以下内容,“对于任何疑问,请随时致电我们的客户支持部门

1 个答案:

答案 0 :(得分:0)

dim s as string' be your input
dim content_array() as string
Dim customer_name As String
Dim customer_requirement As String
Dim customer_phon As String
s = (s.Replace(System.Environment.NewLine, ",").Replace(",,", "")) 'replace enter with comas to create a single string
s = s.Substring(0, s.IndexOf("For any queries"))'eliminate string after "For any queries"
content_array = s.Split(",") 'split the string into array                
For i As Integer For i As Integer = 1 To content_array.Length
If content_array(i) <> "" Then
find_content(content_array(i))'function call
End If
Next

创建数据库连接并插入值;以下函数将为您提供所需的值

Public Sub find_content(ByVal find_string As String)
Dim result() As String
result = find_string.Split(":")
If Trim(result(0).ToString) = "Caller Name" Then
customer_name = result(1).ToString
ElseIf Trim(result(0).ToString) = "Caller Requirement" Then
customer_requirement = result(1).ToString
ElseIf Trim(result(0).ToString) = "Caller Phone" Then
customer_phon = result(1).ToString
End If
End Sub

希望这是你正在寻找的