CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2006
    Posts
    587

    String manipulation

    Hi guys, I'm looking to split my string via the spaces and get all the words apart from the last one. for e.g,

    get_lty("CP GF FGT LEDGER") would give me: CP GF FGT
    get_lty("BLAH 45RT45 BLa 4 bklahg hi") would give me: BLAH 45RT45 BLa 4 bklahg

    can someone help me? thanks

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: String manipulation

    Look at the String class's methods. There is one that will do it for you.
    Norm

  3. #3
    Join Date
    Jul 2010
    Posts
    17

    Post Re: String manipulation

    String [] sections = "CP GF FGT LEDGER".split(" ");
    String finalString ="";
    for(int i=0 ; i<sections.length-1 ; i++){
    finalString += " " + sections[i];
    }
    finalString = finalString.trim();

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: String manipulation

    That's a little long winded why not just use the lastIndexOf() method to find the location of the last space character and then the substring() method to extract the first part of the string.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jul 2010
    Posts
    17

    Re: String manipulation

    Good point keang, use that instead.

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: String manipulation

    Quote Originally Posted by v.z.afzal@dundee.ac.uk View Post
    String [] sections = "CP GF FGT LEDGER".split(" ");
    String finalString ="";
    for(int i=0 ; i<sections.length-1 ; i++){
    finalString += " " + sections[i];
    }
    finalString = finalString.trim();
    While I'm sure the OPs of the couple of posts in which you have given solutions appreciate it, I myself, and a few others here, would appreciate it if you would not so freely perform the work for the OP.

    We are here to help them work towards their goal, not give them free solutions, to what most of the time is a homework assignment. Giving them the answer teaches them nothing.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: String manipulation

    Quote Originally Posted by ProgramThis
    While I'm sure the OPs of the couple of posts in which you have given solutions appreciate it, I myself, and a few others here, would appreciate it if you would not so freely perform the work for the OP.
    Hear hear.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: String manipulation

    I'll third that.
    Norm

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: String manipulation

    I'll add a belated forth

    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25...
    Andrew Rutherford
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: String manipulation

    I'll add a belated forth
    Better late than never
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured