473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String again

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


Oct 2 '07 #1
9 1346
On Oct 2, 10:47 am, "ma" <m...@nowhere.comwrote:
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?

Oct 2 '07 #2
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]
- mv*@spam.guard.caspershouse.com

"ma" <ma@nowhere.comwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
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


Oct 2 '07 #3
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
Oct 2 '07 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u$**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"ma" <ma@nowhere.comwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
>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



Oct 2 '07 #5
ma

<za***@construction-imaging.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
On Oct 2, 10:47 am, "ma" <m...@nowhere.comwrote:
>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

Oct 2 '07 #6
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]
- mv*@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u$**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"ma" <ma@nowhere.comwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
>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



Oct 2 '07 #7

"ma" <ma@nowhere.comwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
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.
Oct 3 '07 #8
bob

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

On Tue, 2 Oct 2007 15:47:34 +0100, "ma" <ma@nowhere.comwrote:
>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
Oct 4 '07 #9
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
On Tue, 2 Oct 2007 15:47:34 +0100, "ma" <ma@nowhere.comwrote:
>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 *?
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.
--
Jesse Houwing
jesse.houwing at sogeti.nl
Oct 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
17129
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
7
26747
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted)...
4
79554
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
17
4634
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
14760
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
1380
by: SeNTry | last post by:
Hi Everyone, My first post here as I just begin to learn programming in general and python in particular. I have all the noobie confused questions, but as I work thru the tutorials I'm sure...
8
8295
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
11
1616
by: solarin | last post by:
Hi, I've the following code: //all variables are defined in the code Class CMessage{ string CMessage::GenerateString() {
21
55569
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
5
3602
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.