473,657 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace parts of a string.

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
8 1552
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.ToChar Array

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

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
On Thu, 2 Jun 2005 21:47:19 +0100, "Aussie Rules"
<so*****@somewh ere.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.Replac e(",","','")

Nov 21 '05 #3
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.ToChar Array

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

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
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(CTyp e(",", 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
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.WriteLi ne(FormatString (inputString))

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

Private Function FormatString(By Val inputString As String) As String
Dim subs() As String = inputString.Spl it(",".ToCharAr ray())
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
Hi,

String.replace
http://msdn.microsoft.com/library/de...placetopic.asp
Ken
---------------
"Aussie Rules" <so*****@somewh ere.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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
On Thu, 02 Jun 2005 21:50:11 GMT, Joe Cool <jo*****@home.n et> wrote:
On Thu, 2 Jun 2005 21:47:19 +0100, "Aussie Rules"
<so*****@somew here.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,4 5,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','6 7','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.Replac e(",","','")


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

myString = "'" & myString.Replac e(",","','") & "'"

Nov 21 '05 #8
Aussie,

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

\\\
mystring = "'" & mystring.replac e(",","'") & "'"
///

I hope this helps,

Cor
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1547
by: Jean-Pierre Bergamin | last post by:
Hi there I want to implement a small templating system where values in angle brackets should be replaced by the corresponding dicitionary value ("foo {x} bar" -> "foo 10 bar"); >>> d = {} >>> d = 10 >>> p = re.compile('{ ( * ) }', re.VERBOSE) >>> p.sub(r'd','foo {x} bar')
4
4663
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and "mno" with another value. Of course the brute-force approach is straight forward. Just iterate over the full string N times (once for "abc", "ghi", and "mno"), find all instances, and replace them using the normal std::string member...
1
3108
by: eysteinbye | last post by:
I have a problem with my RegExp. I need to replace some URLs with other URLs. I am using a URL-rewrite filter). The problem is that when the RegExp match just parts of the string, it does a replace anyway. I want it to only replace if it matches the entire targetstring. Target strings might be like this: 1: /news/public.asp?myid=1234 2: /justadirectory 3: /justadirectory/ 4: /justadirectory/something
5
2629
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char* replace){ char* result; size_t l = strlen(source), r = strlen(replace), i; int number_of_replaces = 0; for(i = 0; i < l; i++){ if(source == search)
6
1478
by: localhost | last post by:
I have a string that looks like this: "document.form1.textBox1.focus ();document.form1.textBox1.select();" I want to replace the text between "document.form1." and ".focus()", as well as the text betwen "document.form1." and ".select ()".
3
5447
by: Andy Sutorius | last post by:
Hi, I read the thread (2/16/05) regarding a replace function in C# however it didn't answer my question. I have a string which is building an insert sql statement and I would like to replace apostrophes of the form fields. I was trying to do something like this: string sqlInsertEmails = "insert into tblContent (content, subject) values ('" + Replace(txtBody.Text,"'","''") + "', '" + Replace(txtSubject.Text,"'","''") + "')";
33
2798
by: cesco | last post by:
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution...
15
1893
by: =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post by:
I have a text file that contains about 8 to 10 text sequences that I need to replace. I want to search and replace all 8 to 10 text sequence anytime I run this script Here is what I have so far. Const ForReading = 1 Const ForWriting = 2
5
2576
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @ shouldn't be replaced.
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.