user704010
user704010

Reputation:

Parse this string in Java

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"

Upvotes: 2

Views: 584

Answers (4)

Roshnal
Roshnal

Reputation: 1314

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.

Upvotes: 1

amit
amit

Reputation: 178521

use String.split():

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

where str contains your String object

Upvotes: 4

yoprogramo
yoprogramo

Reputation: 1306

String line = "Autorul-Stefan";
String [] fields = line.split("-");
// fields[0] == "Autorul"
// fields[1] == "stefan"

Upvotes: 4

Jonas m
Jonas m

Reputation: 2734

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


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

Upvotes: 2

Related Questions