vijay
vijay

Reputation: 1139

format string and get data

I had a string as

Action Completed-PPN-P1:1234

How can I format to get the words separately to check the condition as

if(string == Action Completed)
if(string == PPN)
if......

How can I format to get above code? Thank you.

Upvotes: 1

Views: 741

Answers (4)

WPrecht
WPrecht

Reputation: 1382

You can use the basic library functions:

String entry = "Action Completed-PPN-P1:1234";
String[] splits = entry.split("-");

Which will give you and array of all this:

splits[0] -- "Action Completed"
splits[1] -- "PPN"
splits[2] -- "P1:1234"

Upvotes: 1

Boris the Spider
Boris the Spider

Reputation: 61168

You need to use the String.split method which takes a regex pattern, depending on what you want you need to change the pattern - but to split the string on "-" and ":" you do do this

final String myString = "Action Completed-PPN-P1:1234";

final String[] myStringArr = myString.split("-|:");

System.out.println(Arrays.toString(myStringArr));

Output:

[Action Completed, PPN, P1, 1234]

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388346

Try this

String[] split = "Action Completed-PPN-P1:1234".split("[-:]");
if("Action Completed".equals(split[0])){
if("PPN".equals(split[1])){ 
}

Upvotes: 1

Payal
Payal

Reputation: 913

use .contains method if you want to see if string contains certain characters.For this ,

if(string.contains("Action Completed"))
if(string.contains("PPN"))
if......

Upvotes: 1

Related Questions