2

i'm new to Java . How can i obtain right values of each line (second value of the dash separated pair)

Autorul-Stefan

DenumireaCartii-Popovici

CuloareaCartii-Verde

GenulCartii-Religie

Limba-Rusa

SOLVED :

 String line = "Autorul-Stefan";
    String [] fields = line.split("-");
    fields[0] == "Autorul"
    fields[1] == "stefan"
4
  • 1
    Do you mean "the second value of the dash separated pair"? Commented Oct 15, 2011 at 14:47
  • How can we know which is the 'right value' from your point of view ? :) Commented Oct 15, 2011 at 14:50
  • yes ,the second value of the dash separated pair Commented Oct 15, 2011 at 14:58
  • 1
    @devXcode, if the problem is solved, please accept the answer that solved it instead of adding "(SOLVED)" in the title of your question. Commented Nov 4, 2011 at 18:55

4 Answers 4

4
String line = "Autorul-Stefan";
String [] fields = line.split("-");
// fields[0] == "Autorul"
// fields[1] == "stefan"
Sign up to request clarification or add additional context in comments.

Comments

4

use String.split():

String right = str.split("-")[1];

where str contains your String object

Comments

2
  String strings = "Autorul-Stefan";
  String[] tempo;


  tempo = strings.split("-");
    System.out.println(tempo[1]);

Comments

1

You can use the split() function in Strings:

String rightValue = line.split("-")[1];

Where line is the each line of your text (like "Autorul-Stefan") and rightValue is the text to the right of the dash (like "Stefan").

You use [1] to get the second element of the split text (split separates the given String into an array using the given character (here "-") as a divider) So in this example, the first element of the array is the text to the left of the dash and the second element is the text to the right of the dash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.