Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Getting last word of a string

Getting last word of a string

Scheduled Pinned Locked Moved Java
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tor Danielsen
    wrote on last edited by
    #1

    Working with string and i for something like this

    String test = "this is one test string to show you";

    now how do i fish out the last word of that string ?

    L D D U 4 Replies Last reply
    0
    • T Tor Danielsen

      Working with string and i for something like this

      String test = "this is one test string to show you";

      now how do i fish out the last word of that string ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could try lastIndexOf() string[^] to find the last space, and then use substring() to extract the last part. There are other possibilities depending on your exact requirements. Reading the documentation will always help.

      Unrequited desire is character building. OriginalGriff

      1 Reply Last reply
      0
      • T Tor Danielsen

        Working with string and i for something like this

        String test = "this is one test string to show you";

        now how do i fish out the last word of that string ?

        D Offline
        D Offline
        davidshenba
        wrote on last edited by
        #3

        System.out.println(test.substring(test.lastIndexOf(" ")==-1?0:test.lastIndexOf(" ")+1));

        This one gives you the last word if there is a space, else the word itself if there is no space. Use trim beforehand in case if you expect spaces at the end!

        -Shenbaga Murugan Paramasivapandian I hate computers and I just mess them up with my code!

        1 Reply Last reply
        0
        • T Tor Danielsen

          Working with string and i for something like this

          String test = "this is one test string to show you";

          now how do i fish out the last word of that string ?

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          String test = "this is one test string to show you";
          String[] words = test.split(" ");

          Then just get the last one in the array.

          1 Reply Last reply
          0
          • T Tor Danielsen

            Working with string and i for something like this

            String test = "this is one test string to show you";

            now how do i fish out the last word of that string ?

            U Offline
            U Offline
            User 8318508
            wrote on last edited by
            #5

            Okay,u should get last index of space and last index of " first, then use substring(int beginIndex, int endIndex).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups