-
Replacing a substring at a specific position
Hi all, need some help.
I want to replace the second character in a string, regardless of what character that is.
If I try the replace the second character with the (substring 1, 1) all instances of that character gets replaced.
I just want to replace the one sitting at position 2 in the string.
Ex Good -> Gxod, not Gxxd
Thanks in advance
Patrik
-
paarfal,
One option is to use the classic BASIC MID statement:
Dim myString As String = "abcde"
Mid(myString, 2, 1) = "x"
MsgBox(myString)
(Of course, now the .Net purists will respond that you should not use the MID statement.)
Kerry Moorman
-
In VB, you can use the Mid statement:
Mid(theString, 2, 1) = "x"
There's no equivalent in C#. You could add a reference to the Microsoft.VisualBasic namespace and call the Mid statement from C#, or you could convert the string to an array of characters, update the desired character, then create a new string from the char array.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
In VB, you could have (add semi-colon for C#)
myString = myString.Remove(1, 1).Insert(1, "x")
Of course, not quite as direct as the Mid statement.
-
in C# you can use in this way
TextBox2.Text =TextBox1.Text.Replace(TextBox1.Text.Substring(1,1),"p");
here p is the newly replaced char.
thanks
raj 
-
Raj: I'm pretty sure that will replace all occurrences of the second character with the letter 'p'. So, "good" would become "gppd".
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Hi all,
Thaks for your suggestions. I'll try work it out based on them.
Regards,
Patrik
-
I think the best in C# would be:
TextBox2.Text =TextBox1.Text.Remove(1,1).Insert(1,"p");
cheers
-
why not use LEFT and MID ?
Why couldn't you just use ...
newstring = LEFT(oldstring, 1) & newcharacter & MID(oldstring, 3)
-
That would work, but it's less efficient than Mid(theString, 2, 1) = "x" due to its use of concatenation.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Absolutely, but it is simpler to understand for new programmers. And the performance hit is only noticable if the number of iterations on it are extremely large.
-
OK, how about this:
' Replace second character of theString with 'x'
Mid(theString, 2, 1) = "x"
That's efficient and easy for new programmers to understand (and it doesn't teach them bad habits ;-).
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Works for me! Personally I have always used MID() = X in the manner you describe. Although I wouldn't call the concatenation a "bad habit" ... just not the most efficient one.
-
C# replace specific character
A bit of filthy wat to do it, but it works.
string editString = "Good";
StringBuilder sb = new StringBuilder(editString);
sb.Remove(1, 1);
sb.Insert(1, "x");
editString = sb.ToString();
Similar Threads
-
By eagletonm in forum VB Classic
Replies: 1
Last Post: 10-25-2005, 08:59 AM
-
By pegastaff in forum Careers
Replies: 0
Last Post: 06-14-2005, 11:39 AM
-
By Ranger in forum VB Classic
Replies: 2
Last Post: 12-07-2000, 01:29 PM
-
By Ranger in forum VB Classic
Replies: 0
Last Post: 12-07-2000, 01:23 PM
-
Replies: 2
Last Post: 04-13-2000, 07:22 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks