Results 1 to 6 of 6

Thread: [java] manipulate a string

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [java] manipulate a string

    Hello,
    I have a string containing a fileName and I have to cut the .extension if it there is. otherwise do nothing; furthermore I have to change the first letter to uppercase:
    Is this ok?
    Qt Code:
    1. filename "file.xml" -> File
    2. filename "file" -> File
    To copy to clipboard, switch view to plain text mode 
    I do it but it's not comple......... and it works only string with .extension
    Qt Code:
    1. String s = fileName.substring(0, fileName.indexOf(".")
    To copy to clipboard, switch view to plain text mode 

    Any hints, please?
    Regards

  2. #2
    Join Date
    Jan 2008
    Posts
    72
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: [java] manipulate a string

    Then whats your problem ?????
    can you explain it clearly...............

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [java] manipulate a string

    The problem is I'm new to java.
    Is my way to take out the ".xml" the best way? According to me, if fileName doens't contain an extension (eg. it's only "file") that code doesn't work.
    Furthermore how can I change the first letter of the fileName to upper letter, please?


    Thanks,
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [java] manipulate a string

    How about:

    java Code:
    1. String s = fileName;
    2. if(fileName.indexOf(".")>0)
    3. s = fileName.substring(0, fileName.indexOf(".");
    To copy to clipboard, switch view to plain text mode 

    Just be aware it won't work for file names like "abc.def.ghi". Using lastIndexOf() if java strings have such a thing would be better.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [java] manipulate a string

    and what do you think for the second question? Can this be right?
    Qt Code:
    1. String name = fileName;
    2. if( name.indexOf(".")>0 )
    3. name = name.substring(0, fileName.indexOf("."));
    4. name = name.substring(0,1).toUpperCase() + name.substring(1, name.length() );
    To copy to clipboard, switch view to plain text mode 
    something simpler?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [java] manipulate a string

    It's fine, provided that toUpperCase() can handle the conversion in every case (such conversions sometimes fail with national characters or encodings).

Similar Threads

  1. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.