How to replace letter in a string

Replacing letter in a string (beginner in C#)

Page 1 of 1

8 Replies - 6407 Views - Last Post: 31 August 2009 - 08:19 AM Rate Topic: -----

#1 Baltar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-August 09

How to replace letter in a string

Posted 31 August 2009 - 02:57 AM

Hello. Im new at C#

I have string text="00010000". Yes it should be int but it is used as string. How can i replace "1" with "0" in the string?
i have tried text.substring(3, 1) = "0"; but does not work....
Is This A Good Question/Topic? 0
  • +

Replies To: How to replace letter in a string

#2 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: How to replace letter in a string

Posted 31 August 2009 - 03:28 AM

did you tried String.Replace();

Quick Example:
string str = "000010000";
            str = str.Replace("1", "0");
            MessageBox.Show(str);

Was This Post Helpful? 0
  • +
  • -

#3 Baltar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-August 09

Re: How to replace letter in a string

Posted 31 August 2009 - 03:41 AM

View Postnoorahmad, on 31 Aug, 2009 - 02:28 AM, said:

did you tried String.Replace();

Quick Example:
string str = "000010000";
            str = str.Replace("1", "0");
            MessageBox.Show(str);



sry i should have been more spesific. i need string replace by index. Lets say i have string="0001000101010111" so i need to replace the 4-th letter (1) with 0. Or i need to replace the 8-th letter (1) with 0.
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How to replace letter in a string

Posted 31 August 2009 - 03:49 AM

String is just an zero-based array of chars so you can do this:

myString = "00100";
myString[2] = '0' // set char at position 2 to 0


Was This Post Helpful? 0
  • +
  • -

#5 Baltar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-August 09

Re: How to replace letter in a string

Posted 31 August 2009 - 06:15 AM

View Postjanne_panne, on 31 Aug, 2009 - 02:49 AM, said:

String is just an zero-based array of chars so you can do this:

myString = "00100";
myString[2] = '0' // set char at position 2 to 0





It gives me an error if i do that : Error 8 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: How to replace letter in a string

Posted 31 August 2009 - 06:35 AM

show us your code that produced this error.
Was This Post Helpful? 0
  • +
  • -

#7 Baltar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-August 09

Re: How to replace letter in a string

Posted 31 August 2009 - 06:40 AM

string rights = "00000000000000000";
if(checkbox1.IsChecked == true)
{
rights[5] = "1";
}
Was This Post Helpful? 0
  • +
  • -

#8 Ikonmx   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 19-August 09

Re: How to replace letter in a string

Posted 31 August 2009 - 07:11 AM

If you're using binary flags.you could use bit array class
Was This Post Helpful? 0
  • +
  • -

#9 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How to replace letter in a string

Posted 31 August 2009 - 08:19 AM

Sorry, I indeed provided you with false information in my previous post: you can't change the value of the char like that.

Ikonmx's idea of using bit array is good but if that doesn't suit your needs, here is sample how you could do it with Extension methods. Or if you don't want to use extension, you can easily modify the code to suit your needs:

namespace MyNamespace
{
	class Program
	{
		static void Main(string[] args)
		{
			string str = "00001000";
			str = str.ChangeCharacter(4, "0");

			// result: "00000000"
		}
	}

	public static class StringExtensions
	{
		public static string ChangeCharacter(this String str, int zeroBasedPosition, string newChar)
		{
			string newStr = str.Remove(zeroBasedPosition, 1);
			newStr = newStr.Insert(zeroBasedPosition, newChar);
			return newStr;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1