By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Replace parts of a string.

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

"Aussie Rules" wrote:
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

Nov 21 '05 #2

P: n/a
On Thu, 2 Jun 2005 21:47:19 +0100, "Aussie Rules"
<so*****@somewhere.com> wrote:
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(",","','")

Nov 21 '05 #3

P: n/a
I forgot to clear the contents of 'thenums' variable. So, just before the For
loop,

thenums = ""

"Mark" wrote:
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

"Aussie Rules" wrote:
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

Nov 21 '05 #4

P: n/a
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)

"Aussie Rules" wrote:
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

Nov 21 '05 #5

P: n/a
In article <#A**************@TK2MSFTNGP15.phx.gbl>, Aussie Rules wrote:
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
--
Tom Shelton [MVP]
Nov 21 '05 #6

P: n/a
Hi,

String.replace
http://msdn.microsoft.com/library/de...placetopic.asp
Ken
---------------
"Aussie Rules" <so*****@somewhere.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #7

P: n/a
On Thu, 02 Jun 2005 21:50:11 GMT, Joe Cool <jo*****@home.net> wrote:
On Thu, 2 Jun 2005 21:47:19 +0100, "Aussie Rules"
<so*****@somewhere.com> wrote:
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(",","','") & "'"

Nov 21 '05 #8

P: n/a
Aussie,

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

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

I hope this helps,

Cor
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.