String again

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
Z

zacks

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?
 
H

Hans Kesting

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
 
M

ma

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
 
M

ma

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
 
N

Nicholas Paldino [.NET/C# MVP]

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
 
B

Ben Voigt [C++ MVP]

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.
 
B

bob

Hi ma,
If your string varies then perhaps you should be using regular
expressions to find and replace. See regex class
regards
Bob
 
J

Jesse Houwing

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top