-2

I having test file in my java project directory which having below content:

HEADER|INPUT|2017|test|1 |Id|Name|

From where I want to update " Id " value from another string "xyz"

"ID" is not static everytime this value gets changed

How can I get particular string using java?

kavya
  • 15
  • 1
  • 5
  • 1
    @user7722867 Won't work. The pipe is a special character in regex. – MC Emperor Mar 22 '19 at 11:12
  • 1
    Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Lino Mar 22 '19 at 11:13

3 Answers3

2

You can use the split function. This will return your string split into the parts, seperated by |.

var result = "HEADER|INPUT|2017|test|1 |Id|Name|".split("\\|");
// Access an array
// result[0] will be 'Header'

Source

Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43
2

You can use String.split() function:

String string = "HEADER|INPUT|2017|test|1 |Id|Name|";
String[] parts = string.split("\\|");
String part1 = parts[0]; // HEADER
String part2 = parts[1]; // INPUT
String part3=parts[2]; //2017

and so on.

0

You can make use of split() method here to divide your string as per your requirement.

String input= "HEADER|INPUT|2017|test|1 |Id|Name|";
String[] contents = input.split("\\|");
String name = input[6]; 
String id = input[5]; 
String number = input[4]; 
String test = input[3];  
String year = input[2];  
String input = input[1]; 
String header = input[0];

For more on String.split()

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26