Click here to Skip to main content
15,913,722 members
Home / Discussions / C#
   

C#

 
GeneralRe: Replace a letter in a Textbox? Pin
Luc Pattyn28-Nov-09 11:28
sitebuilderLuc Pattyn28-Nov-09 11:28 
GeneralRe: Replace a letter in a Textbox? Pin
ahlm28-Nov-09 11:42
ahlm28-Nov-09 11:42 
GeneralRe: Replace a letter in a Textbox? Pin
Luc Pattyn28-Nov-09 11:54
sitebuilderLuc Pattyn28-Nov-09 11:54 
GeneralRe: Replace a letter in a Textbox? Pin
ahlm28-Nov-09 13:18
ahlm28-Nov-09 13:18 
GeneralRe: Replace a letter in a Textbox? Pin
Luc Pattyn28-Nov-09 14:07
sitebuilderLuc Pattyn28-Nov-09 14:07 
GeneralRe: Replace a letter in a Textbox? Pin
DaveyM6928-Nov-09 21:53
professionalDaveyM6928-Nov-09 21:53 
GeneralRe: Replace a letter in a Textbox? Pin
ahlm28-Nov-09 22:23
ahlm28-Nov-09 22:23 
GeneralRe: Replace a letter in a Textbox? Pin
DaveyM6929-Nov-09 1:55
professionalDaveyM6929-Nov-09 1:55 
That will work but it's not very versatile - if you just need to replace the word 'script'with 'scripl' then you should just use that part
C#
string str = "codeproject.com/script/";
str = str.Replace("script", "scripl");
This doesn't deal with your original problem though, replacing one character a specific position with another. You could hard code something to alter just the 29th to 't', but then it wouldn't be reusable. I would create a method like
C#
public static string ReplaceCharacter(string original, int index, char newChar)
{
    // ...
}
There are several methods which you will have come across through the google link.
There is String.Replace, but that replaces all occurences of a character or string inside the string plus you can't specify a position.
You could build a new string using a StringBuilder using the Substring up to the required character, appending the character you want and then appending the Substring after it. This would work but is a bit messy.
The method I would prefer is to convert the string to a char[], change the character at the correct index then create a new string from the array. This would look something like this:
C#
// Characters must be Unicode!
public static string ReplaceCharacter(string original, int index, char newChar)
{
    if (index < original.Length)
    {
        char[] chars = original.ToCharArray();
        chars[index] = newChar;
        return new string(chars);
    }
    else
        return original;
}


Dave

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

QuestionWhen to call base.Onstart Pin
Jordanwb28-Nov-09 8:13
Jordanwb28-Nov-09 8:13 
AnswerRe: When to call base.Onstart Pin
DaveyM6928-Nov-09 8:58
professionalDaveyM6928-Nov-09 8:58 
GeneralRe: When to call base.Onstart Pin
Luc Pattyn28-Nov-09 9:23
sitebuilderLuc Pattyn28-Nov-09 9:23 
GeneralRe: When to call base.Onstart Pin
DaveyM6928-Nov-09 11:02
professionalDaveyM6928-Nov-09 11:02 
GeneralRe: When to call base.Onstart [modified] Pin
Jordanwb6-Dec-09 15:08
Jordanwb6-Dec-09 15:08 
AnswerRe: When to call base.Onstart Pin
PIEBALDconsult28-Nov-09 14:18
mvePIEBALDconsult28-Nov-09 14:18 
QuestionPrint by windows CE device Pin
Sajjad Izadi28-Nov-09 7:12
Sajjad Izadi28-Nov-09 7:12 
AnswerRe: Print by windows CE device Pin
Christian Graus28-Nov-09 7:42
protectorChristian Graus28-Nov-09 7:42 
GeneralRe: Print by windows CE device Pin
Sajjad Izadi28-Nov-09 7:45
Sajjad Izadi28-Nov-09 7:45 
QuestionPrinting a datagridview... Pin
JollyMansArt28-Nov-09 7:05
JollyMansArt28-Nov-09 7:05 
QuestionRe: Printing a datagridview... Please help Pin
JollyMansArt28-Nov-09 10:03
JollyMansArt28-Nov-09 10:03 
QuestionGetting Report from dataBase and showing it in Word. Pin
Said Ali Jalali28-Nov-09 6:18
Said Ali Jalali28-Nov-09 6:18 
AnswerRe: Getting Report from dataBase and showing it in Word. Pin
dan!sh 28-Nov-09 6:31
professional dan!sh 28-Nov-09 6:31 
AnswerRe: Getting Report from dataBase and showing it in Word. Pin
minnie mouse1-Dec-09 12:50
minnie mouse1-Dec-09 12:50 
QuestionWCF catching EndPointNotFoundException Pin
Ryan Minor28-Nov-09 6:00
Ryan Minor28-Nov-09 6:00 
AnswerRe: WCF catching EndPointNotFoundException Pin
Ravi Bhavnani28-Nov-09 7:51
professionalRavi Bhavnani28-Nov-09 7:51 
QuestionHow to decide whether a class is "internal" by .net reflection ? Pin
yoyota28-Nov-09 4:26
yoyota28-Nov-09 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.