Finding a string within a string

Need to search a string of characters and find a string within the str

Page 1 of 1

7 Replies - 12205 Views - Last Post: 10 October 2008 - 11:35 AM Rate Topic: -----

#1 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Finding a string within a string

Posted 09 October 2008 - 09:37 AM

I am building an application that will connect to an access data base, and am trying to build a function into the add/update that will prevent duplicate entries or signatures, but allow updates. IE if the database record contains: AB CD EF, the function needs to be able to determine that CD is already entered.

I have tried using the vbTextCompare and the code below (i applogize that i lost the post where i had found the code from the compare function)

Both were able to find the whole string, but not to pick out a single entry.

Any ideas?

Thank you for the help

Imports System.Data
Imports System.Data.OleDb
Public Class frmCaseAdmin

	Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

		Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\My Documents\CaseAdmin.accdb")
		conn.Open()

		Dim userDetailsAdapter As New OleDbDataAdapter("SELECT * FROM tblCaseAdmin where Digit =" + cboDigit.Text, conn)

		Dim userDetailsDataSet As New DataSet
		userDetailsAdapter.Fill(userDetailsDataSet)
		userDetailsAdapter.Dispose()
		Dim userDetailsTable As DataTable = userDetailsDataSet.Tables(0)

		Dim stringFromForm As String

		Compare(txtCasesReport.Text, userDetailsTable, 3)
		' If InStr(1, UCase(txtCasesReport.Text), UCase(userDetailsTable.Rows(0).Item(3)), vbTextCompare) > 0 Then
		'MsgBox("Text is already present")
		txtCasesReport.Text = userDetailsTable.Rows(0).Item(3)
		'Else
		'MsgBox("Text is not found")
		'End If

		conn.Close()
	End Sub

	Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub

	Private Sub Compare(ByVal stringFromForm, ByVal userDetailsTable, ByVal position)
		' Multi-line string  
		Dim stringToCheck As String = userDetailsTable.Rows(0).Item(position)

		' Singular-line string  
		Dim stringToCompare As String = stringFromForm.ToString

		' Create a new StringReader object  
		Dim strRead As New System.IO.StringReader(stringToCheck)

		' Start reading the String  
		While strRead.Peek() > 0
			' Condition to check if a line in the multi-line string = the string to <SPAN class=searchlite>compare</SPAN> to  
			If strRead.ReadLine() = stringToCompare Then
				MsgBox("String was found")
				Exit While
			Else 
				MsgBox("String not found")
			End If

		End While
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Finding a string within a string

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8383
  • View blog
  • Posts: 31,159
  • Joined: 12-June 08

Re: Finding a string within a string

Posted 09 October 2008 - 01:03 PM

Two ways:

1.
Your stored procedure in Access can take care of this... (assuming you are using a stored procedure). Call it 'save' or something akin to this.

Inside the stored procedure you would declare an int variable that is used in a count-select statement and the where would be a string build with your string surrounded by % signs. If the count is greater than zero then you need to do an insert.. else do an update.

I am guessing you are storing "AB CD EF" as a string in a row. Let's call this column sVal. Your key column will be lVal.

You have an incoming string value of CD. You want to see if CD is in the "AB CD EF". This incoming variable will be called sIncoming

inside your stored proc:
DECLARE @lCount INT
SET @lCount  = 0

SELECT @lCount   = COUNT(lVal)
FROM <your table> WITH(NOLOCK)
WHERE sVal LIKE '% ' + sIncoming + ' %'

IF @lCount  > 0
BEGIN
-- do update
END
ELSE
BEGIN
-- do insert
END


---------
2. the alternatve method would be a stored procedure funtion that is called in your VB code. This stored proc would consist of the stuff above the line with the IF @lCount > 0 and would return true or false if the record with that string value exists already. From here your VB could would either run the insert or update stored procedure.

It's all a matter of where you want to do the check.
Was This Post Helpful? 0
  • +
  • -

#3 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Finding a string within a string

Posted 09 October 2008 - 01:42 PM

yet again i reply without reading the whole thing.

Quote

Both were able to find the whole string, but not to pick out a single entry.

what do you mean by this? I thought instr was able to pick up parts of a string?

This post has been edited by Damage: 09 October 2008 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Finding a string within a string

Posted 09 October 2008 - 02:25 PM

View PostDamage, on 9 Oct, 2008 - 01:42 PM, said:

yet again i reply without reading the whole thing.

Quote

Both were able to find the whole string, but not to pick out a single entry.

what do you mean by this? I thought instr was able to pick up parts of a string?


If the row contained AB CD EF, and i entered CD it would be false. If i entered AB CD EF it would be true.

Maybe my syntax on the instr is off?
Was This Post Helpful? 0
  • +
  • -

#5 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Finding a string within a string

Posted 09 October 2008 - 07:19 PM

nope no you didn't. I'm actually trying to write a function to do the similar thing as you(find text within a file) and just testing the instr function. And from what i can tell it has to match the whole line. Which makes the name kinda misleading. Unless i'm also doing something wrong? Anyone?
Was This Post Helpful? 0
  • +
  • -

#6 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Finding a string within a string

Posted 09 October 2008 - 08:19 PM

 
Dim str As String = "AB CD EF"
		Dim newstr As String = "CD"

		Dim r As Integer = -1
		r = str.IndexOf(newstr)
		If not r = -1 Then
			MsgBox("hello")

		End If


This post has been edited by Damage: 09 October 2008 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Finding a string within a string

Posted 09 October 2008 - 08:26 PM

Why not just use the Contains method of the String class to test and see if the value exists?

Assuming that you will be searching for a pair of characters or a series of characters then you could test for it like this:

	Private Sub Compare(ByVal stringFromForm, ByVal userDetailsTable, ByVal position)
		' Multi-line string  
		Dim stringToCheck As String = userDetailsTable.Rows(0).Item(position)

		' Singular-line string  
		Dim stringToCompare As String = stringFromForm.ToString

		If stringToCompare.Contains(stringToCheck) Then
		   MessageBox.Show("String was found")
		Else
		   MessageBox.Show("String was not found")
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#8 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Finding a string within a string

Posted 10 October 2008 - 11:35 AM

View Postjayman9, on 9 Oct, 2008 - 08:26 PM, said:

Why not just use the Contains method of the String class to test and see if the value exists?

Assuming that you will be searching for a pair of characters or a series of characters then you could test for it like this:


Thanks this method worked for me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1