String again

  • Thread starter Thread starter ma
  • Start date Start date
M

ma

Hello,
I want to change one character in a string to a new character. How can i
do this. For example I have a string called x and it contains "This is a
test" and I want to change the i in is to a so after change it become "This
as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?



Regards
 
ma,

You will have to return the new string. Strings are immutable, so you
can't change the contents (at least, not without bending over backwards).
You really want to do this:

public void ChangeChar(int index, char newChar)
{
// Remove the character first.
m_MyString = m_MyString.Remove(index);

// Insert the character.
m_MyString = m_MyString.Insert(index, newChar);
}

I changed the code so that you are changing the same variable. You were
not doing that in your original posted code.
 
Hello,
I want to change one character in a string to a new character. How can i
do this. For example I have a string called x and it contains "This is a
test" and I want to change the i in is to a so after change it become "This
as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?

Regards

I take it you missed the String.Replace method when you searched the
help text?
 
ma used his keyboard to write :
Hello,
I want to change one character in a string to a new character. How can i
do this. For example I have a string called x and it contains "This is a
test" and I want to change the i in is to a so after change it become "This
as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?



Regards

As mentioned before, you can not change the contents of a String. You
can however build a new string and replace the previous reference.
So build a new string: part before the insert, the new char, part after
the insert.

You might also want to look into StringBuilder (namespace System.Text),
which does have an Insert method.

Hans Kesting
 
Thanks,
But the code doesn't compile with error code that it can not convert char to
string.
How can I change char to string? Note that it is not char * but char.

Regards




Nicholas Paldino said:
ma,

You will have to return the new string. Strings are immutable, so you
can't change the contents (at least, not without bending over backwards).
You really want to do this:

public void ChangeChar(int index, char newChar)
{
// Remove the character first.
m_MyString = m_MyString.Remove(index);

// Insert the character.
m_MyString = m_MyString.Insert(index, newChar);
}

I changed the code so that you are changing the same variable. You
were not doing that in your original posted code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ma said:
Hello,
I want to change one character in a string to a new character. How can
i do this. For example I have a string called x and it contains "This is
a test" and I want to change the i in is to a so after change it become
"This as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?



Regards
 
I take it you missed the String.Replace method when you searched the
help text?

Thanks.
Replace change all occurrence of the char with new ones but I want to
replace one specific one with a new one.

Regards
 
You can call ToString on the character, like so:


public void ChangeChar(int index, char newChar)
{
// Remove the character first.
m_MyString = m_MyString.Remove(index);

// Insert the character.
m_MyString = m_MyString.Insert(index, newChar.ToString());
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
ma,

You will have to return the new string. Strings are immutable, so you
can't change the contents (at least, not without bending over backwards).
You really want to do this:

public void ChangeChar(int index, char newChar)
{
// Remove the character first.
m_MyString = m_MyString.Remove(index);

// Insert the character.
m_MyString = m_MyString.Insert(index, newChar);
}

I changed the code so that you are changing the same variable. You
were not doing that in your original posted code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ma said:
Hello,
I want to change one character in a string to a new character. How can
i do this. For example I have a string called x and it contains "This is
a test" and I want to change the i in is to a so after change it become
"This as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?



Regards
 
ma said:
Hello,
I want to change one character in a string to a new character. How can
i do this. For example I have a string called x and it contains "This is a
test" and I want to change the i in is to a so after change it become
"This as a test". I tried the following code without any sucess:

public void ChangeChar(int index,char NewChar)

{

m_MyString.Remove(Index);

m_Privilage.Insert(Index,NewChar);

}

How can I convert a char to a string? How can I convert a char to char *?

See string.ToCharArray(), once you have an array you can overwrite any
character at will.
 
Hi ma,
If your string varies then perhaps you should be using regular
expressions to find and replace. See regex class
regards
Bob
 
Hello Bob,
Hi ma,
If your string varies then perhaps you should be using regular
expressions to find and replace. See regex class
regards
Bob

As a string is an immutable type in .NEt you'd have to change your code a
bit:

public string ChangeChar(int index,char NewChar)
{
return m_MyString.Remove(Index).Insert(Index,NewChar);
}


Or you might be able to use a ref parameter:

public void ChangeChar(ref string input, int index,char NewChar)
{
input = input.Remove(Index).Insert(Index,NewChar);
}

Also consider that if you're going to search and replace many characters
in your string, it might be much faster to do all the manipulations on a
StringBuilder instead.
 
Back
Top