Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

working with strings c#

Options
  • 30-07-2007 10:13am
    #1
    Closed Accounts Posts: 2,616 ✭✭✭


    I have a string, and wnat to repalce spaces with underscores. i have tried this but it doesnt work

    string strTempFileName = strFilename;
    for(int i=0; i<strFilename.Length; i++)
    {
    if(Convert.ToChar(strFilename) == ' ')
    {
    strTempFileName.Remove(i,5);
    strTempFileName.Insert(i,"yer ma");
    }
    }
    strTempFileName = strTempFileName;

    any ideas?


Comments

  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    I have a string, and wnat to repalce spaces with underscores. i have tried this but it doesnt work

    string strTempFileName = strFilename;
    for(int i=0; i<strFilename.Length; i++)
    {
    if(Convert.ToChar(strFilename) == ' ')
    {
    strTempFileName.Remove(i,5);
    strTempFileName.Insert(i,"yer ma");
    }
    }
    strTempFileName = strTempFileName;

    any ideas?

    what is it doing? is it running?

    not a c# head but this is fairly simple.

    in pseudocode
    begin
        str := "some sort of sentence."
    
        for i in str.length
            if str[i] == " "
            then
            str[i] = "_"
        
        print str
    end
    

    also wrap code in [.code][./code] - minus the dots - to improve readability.


  • Registered Users Posts: 981 ✭✭✭fasty


    There's a function in class String called Replace that will do this for you.


  • Closed Accounts Posts: 2,616 ✭✭✭8k2q1gfcz9s5d4


    fasty wrote:
    There's a function in class String called Replace that will do this for you.

    tried that and it didnt work, it wouldnd work with white space for some reason.
    got it working a neway. thanks


  • Registered Users Posts: 604 ✭✭✭Kai


    Strings are immutable. you need to call it like:

    string str2 = str1.Remove(0, 5);


  • Closed Accounts Posts: 2,616 ✭✭✭8k2q1gfcz9s5d4


    Kai wrote:
    Strings are immutable. you need to call it like:

    string str2 = str1.Remove(0, 5);

    yes, i remember now!


  • Advertisement
Advertisement