Babu
Babu

Reputation: 169

How to extract a specific text from the string variable in scala

I am new to Spark scala world and trying to learn it. I have a variable containing following value.

val result = "Scan value: 1802 seconds
Start time: Thu, Jan 31 2019, 15:09:09 GMT
End time: Thu, Jan 31 2019, 15:39:11 GMT"

I want to read scan value which is 1802 seconds and start time and End Time to different variables.

I am trying to use substring but I am not getting the results properly and the position may very the way the user enters.

For example: To read scan value, I am doing as follows. But some times these position might change based on how user enters in the system.

val scan_value = result.sbstring(13,4)

Could anyone please help me on how to read these values into separate variables in scala.

Thanks, Babu

Upvotes: 2

Views: 1778

Answers (2)

det0
det0

Reputation: 281

You can also try the following if the different values are always separated by newline.

val result = """Scan value: 1802 seconds
Start time: Thu, Jan 31 2019, 15:09:09 GMT
End time: Thu, Jan 31 2019, 15:39:11 GMT"""

val resultArray = result.split('\n')
val scanValue = resultArray.find(_.startsWith("Sc")).map(_.stripPrefix("Scan value: ").stripSuffix(" seconds"))
val startTime = resultArray.find(_.startsWith("St")).map(_.stripPrefix("Start time: "))
val endTime = resultArray.find(_.startsWith("E")).map(_.stripPrefix("End time: "))

Upvotes: 0

Chaitanya
Chaitanya

Reputation: 3638

Assuming the input will always be in the specified format, the following code will help you to extract out startTime and end time to their respective variables

val result = """ Scan value: 1802 seconds
Start time: Thu, Jan 31 2019, 15:09:09 GMT
  End time: Thu, Jan 31 2019, 15:39:11 GMT"""

val mayBeScanTime = result.split("Scan value:").find(_.contains("seconds")).map(_.split("seconds")(0).trim)


val startTime = Option(result.split("Start time:")(1).split("End time:")(0).trim)


val endTime = Option(result.split("End time:")(1).trim)

This will give you an output as

mayBeScanTime: Option[String] = Some(1802)


startTime: Option[String] = Some(Thu, Jan 31 2019, 15:09:09 GMT)


endTime: Option[String] = Some(Thu, Jan 31 2019, 15:39:11 GMT)

Please note that if the input string changes, the following code would not produce the correct result.

Upvotes: 2

Related Questions