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

Replace First Occurrance of a substring

P: n/a
Hi all,

String.Replace replaces all occurrances of a substring in a given string .

Can anyone guide me how can I replace first occurrance of a substring in a
given string.

-
wa
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Use a Regex expression

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"Wasim Akram" <a@b.c> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
Hi all,

String.Replace replaces all occurrances of a substring in a given string .

Can anyone guide me how can I replace first occurrance of a substring in a
given string.

-
wa

Nov 21 '05 #2

P: n/a
Wasim,
In addition to the other comments I would do an String.IndexOf along with
String.Remove & String.Insert to manually change the string.

Something like:
Public Shared Function ReplaceFirst(ByVal expression As String, ByVal
find As String, ByVal replacement As String) As String
Dim index As Integer = expression.IndexOf(find)
Return expression.Remove(index, find.Length).Insert(index,
replacement)
End Function

Hope this helps
Jay

"Wasim Akram" <a@b.c> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
Hi all,

String.Replace replaces all occurrances of a substring in a given string .

Can anyone guide me how can I replace first occurrance of a substring in a
given string.

-
wa

Nov 21 '05 #3

P: n/a
Wasim,

I like those questions where more answers are possible.

\\\
Dim a As String = "terryjaycorherfriedcorjayterry"
Dim b As String = Replace(a, "jay", "jayb", 1, 1)
///

I hope this helps?

Cor
Nov 21 '05 #4

P: n/a
Cor,
Doh!

You know I looked quickly at VB.Replace to get the parameters for mine. I
did not notice (nor remember) the Count (occurances) parameter. I'm sure I
saw count & thought number of characters to look in...

FWIW: You can actually leave the start parameter off.

Dim b As String = Replace(a, "jay", "jayb", , 1)

or

Dim b As String = Replace(a, "jay", "jayb", Count := 1)

Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:O6*************@TK2MSFTNGP14.phx.gbl...
Wasim,

I like those questions where more answers are possible.

\\\
Dim a As String = "terryjaycorherfriedcorjayterry"
Dim b As String = Replace(a, "jay", "jayb", 1, 1)
///

I hope this helps?

Cor

Nov 21 '05 #5

P: n/a
Jay,

I was writting a funny one using the split, when I remembered me this one,
and would make a message in the kind that Herfried write the replace ones
normally (it was more Armin) and I the strange one, however maybe the OP
would have taken that split one as a real one as once happened and than it
was a problem, so I only provided this Replace one.

For you in pseudo so they will not take it as a right one the splitting one
was, splitting, take the first string, the replacement and than the rest of
the strings() concatinated again from 2 to the end.

Cor
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.