Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

working with strings c#

  • 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, Registered Users 2 Posts: 26,449 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 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