-2

I want to manipulate this string:

"Roger:Rabbit:22:California"

and display the output as follows:

Name: Roger Rabbit
Age: 22
State: California

I am wondering what will be the best approached to this?

  • 2
    This is not a code writing service. What have you tried so far to do this yourself? – Ken White Mar 02 '17 at 01:59
  • Also, unless I'm missing something here, you provided no context. From your profile I'm guessing you'd want to work in Java? - Your tag also mentions stringtokenzier, which is a Java, though "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead." - http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html – Julix Mar 02 '17 at 02:05

1 Answers1

1

For this you may use the String split() method. Mention your delimiter as :. Then you want to make sure you create an array out of the output of your split method with the different strings split. In your System.out.println() you can mention the array index of your newly created array elements as System.out.println("Name" + myIndex[0]). Resource: https://www.tutorialspoint.com/java/java_string_split.htm

chromechris
  • 215
  • 4
  • 9
  • 1
    Wouldn't first name and last name get each their own line this way? - nevermind, [0] not [i], you weren't looping. So ("Name: " + myIndex[0] + " " + myIndex[1]) etc. – Julix Mar 02 '17 at 02:10
  • 1
    @Julix I was giving one example. Indeed the Array would contain all elements from the original String that was parsed by the `split()` method. Didn't want to give it all away. – chromechris Mar 02 '17 at 03:46
  • 1
    Thanks, I felt that using the split() method would be the best method. I just need to be sure before going forward with this process. – Markis Bradley Guye Mar 02 '17 at 14:47