Replace parts of a string.

  • Thread starter Thread starter Aussie Rules
  • Start date Start date
A

Aussie Rules

Hi,

I have a string that is built on the fly, and often of different lengths.
The string contains a series of numbers with a comma seperating each number.

For example

'2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9'

The problem I have is that i am passing this into a stored procedure 'in'
clause, and it requires that the numbers be in this format

'2','4','7','8','12','45','67','8','9','33' or '2','6',9'.

Is there a quick way to replace the , with a ',' string, keeping in mind
that the length and number of replaces is always going to be different.

Thanks
 
I had a similar problem. The way I got around it was to put the numbers into
an array using the split method. Then cycle through the array and format a
string. Probably a more elegant way to do it, but it works.

'Define the character that delimits the numbers
Dim delstr As String = ","

'Define an example string of numbers
Dim thenums As String = "2,3,4,5,6"

'Define the delimiter as a char type
Dim delimit As Char() = delimStr.ToCharArray

'Define an array as string and put the numbers into it
Dim aNums() as string = thenums.Split(delimit)

Dim x As Integer
For x = 0 To UBound(aNums)
If x < UBound(aNums) Then
thenums = thenums & "'" & aNums(x) & "',"
Else
thenums = & thenums & "'" & aNums(x) & "'"
End If
Next
 
I forgot to clear the contents of 'thenums' variable. So, just before the For
loop,

thenums = ""
 
Imports System.Text
....
Dim str1 As String = "1,2,3,4,5"
Dim str2 As String(), i As Integer, str3 As StringBuilder
str2 = str1.Split(CType(",", Char))
str3 = New StringBuilder
For i = 0 To str2.Length - 1
str3.Append(Chr(39) & str2(i) & Chr(39) & ",")
Next
str1 = str3.ToString(0, str3.Length - 1)
 
Hi,

I have a string that is built on the fly, and often of different lengths.
The string contains a series of numbers with a comma seperating each number.

For example

'2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9'

The problem I have is that i am passing this into a stored procedure 'in'
clause, and it requires that the numbers be in this format

'2','4','7','8','12','45','67','8','9','33' or '2','6',9'.

Is there a quick way to replace the , with a ',' string, keeping in mind
that the length and number of replaces is always going to be different.

Thanks

How about:

dim myString as String

myString = "2, 6, 9"
..
..
..
myString = myString.Replace(",","','")
 
Hi,

I have a string that is built on the fly, and often of different lengths.
The string contains a series of numbers with a comma seperating each number.

For example

'2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9'

The problem I have is that i am passing this into a stored procedure 'in'
clause, and it requires that the numbers be in this format

'2','4','7','8','12','45','67','8','9','33' or '2','6',9'.

Is there a quick way to replace the , with a ',' string, keeping in mind
that the length and number of replaces is always going to be different.

Thanks

Rough example with no input validation or error checking :)

Option Strict On
Option Explicit On

Module Module1

Sub Main()
Dim inputString As String = "2,4,7,8,12,45,67,8,9,33"
Console.WriteLine(FormatString(inputString))

inputString = "2,6,9"
Console.WriteLine(FormatString(inputString))
End Sub

Private Function FormatString(ByVal inputString As String) As String
Dim subs() As String = inputString.Split(",".ToCharArray())
Return String.Format("'{0}'", String.Join("','", subs))
End Function
End Module


Output:
'2','4','7','8','12','45','67','8','9','33'
'2','6','9'


HTH
 
Hi,

String.replace
http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclassreplacetopic.asp


Ken
---------------
Hi,

I have a string that is built on the fly, and often of different lengths.
The string contains a series of numbers with a comma seperating each number.

For example

'2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9'

The problem I have is that i am passing this into a stored procedure 'in'
clause, and it requires that the numbers be in this format

'2','4','7','8','12','45','67','8','9','33' or '2','6',9'.

Is there a quick way to replace the , with a ',' string, keeping in mind
that the length and number of replaces is always going to be different.

Thanks
 
How about:

dim myString as String

myString = "2, 6, 9"
.
.
.
myString = myString.Replace(",","','")

Of course this will only replace the commas. For your case a more
complete answer is:

myString = "'" & myString.Replace(",","','") & "'"
 
Aussie,

Assuming it is "2,6,9" and really has to be a ' and not a ""

\\\
mystring = "'" & mystring.replace(",","'") & "'"
///

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top