7 Replies - 83053 Views - Last Post: 26 February 2018 - 11:34 AM Rate Topic: -----

#1 nmgod   User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Replace Char at specified index of string

Posted 01 December 2009 - 09:47 PM

I want to replace a char at a specified index of a string.

Short of using the .Remove and .Insert methods I was wondering if there is an easier way.
Would be nice if the .Char array was not readonly.

Like id like to do something like

Data(0) = cchar("w")

But I know thats readOnly and Id like to make it easier then using .remove and .insert
Is This A Good Question/Topic? 0
  • +

Replies To: Replace Char at specified index of string

#2 mark.bottomley   User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Replace Char at specified index of string

Posted 02 December 2009 - 08:20 AM

String.Replace should do what you want - just remember that all string are immutable (unchangable) so the returned string is a new string containing the modifications.
Was This Post Helpful? 0
  • +
  • -

#3 nmgod   User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: Replace Char at specified index of string

Posted 02 December 2009 - 11:08 AM

No the .Replace method is used to replace all chars of a certain type in the string, eg.

		Dim a As String = "hello"
		MsgBox(a.Replace(CChar("l"), CChar("d")))



Swould show "heddo"

i want for it to be like, "hedlo"

This post has been edited by nmgod: 02 December 2009 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Replace Char at specified index of string

Posted 02 December 2009 - 11:19 AM

You could use the StringBuilder class

Public Function ChangeCharacter(s As String, replaceWith As Char, idx As Integer) As String
	Dim sb As New StringBuilder(s)

	'now do some replacing
	sb(idx) = replaceWith

	Return sb.ToString()
End Function


Just an idea/option :)
Was This Post Helpful? 0
  • +
  • -

#5 nmgod   User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: Replace Char at specified index of string

Posted 02 December 2009 - 11:59 AM

As I figured, no one-liners, dam, Owell thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Replace Char at specified index of string

Posted 02 December 2009 - 12:30 PM

Are you talking about something like?:
		Dim str As String = "ABCDEFGHJJKL"
		str = str.Remove(8, 1).Insert(8, "I")
		MsgBox(str)


This removes the 9th character (in this case an extra J at index 8) and inserts the letter I so that the string returned is ABCDEFGHIJ

Sorry, did read your post correctly, I think you could do something like this using strings.

   Dim str as String = "ABCDEFGHJJKL"
   str = Strings.Replace(str, "J", "I", 1, 1)
   msgbox(str)



This should replace the first J with the letter I and only replace the 1st occurrence.

This post has been edited by CharlieMay: 02 December 2009 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley   User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Replace Char at specified index of string

Posted 02 December 2009 - 12:55 PM

you can also use .SubString the 2 parts of the string and concatenate them around the new character.
Was This Post Helpful? 0
  • +
  • -

#8 tolsen64   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-February 18

Re: Replace Char at specified index of string

Posted 26 February 2018 - 11:34 AM

I realize i'm a little late to this party, but works fine in VB 2017:

Mid(Text, 3, 1) = "a" 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1