Get sub-string from text based on certain conditions rules

0 votes

I have a string like:

17:27 - 13:10(+1 day) Trip duration 13h43 New York (JFK) 1 x transfer Includes travel by bus Maastricht (ZYT) Lowest fare USD 862 View flight details 

I want to get certain texts from the string like departing time or price. But the strings can be of different lengths and texts so I need rules to be able to get the sub-strings I need.

What I know: Times are formatted like 17:27 - 13:10 with depart time on left and arrival time on the right of -, locations as New York (JFK) where the first is depart location and the second is arrival location, transfers are a number followed by an x like 1 x, and a price is a number that comes after USD text like USD 862

So in the above example text, the data I want to extract from the string is: 17:27, 13:10, New York (JFK), 1, Maastricht (ZYT), and 862

To explain it better, say I know a specific string like the one I gave above, I would like to extract it like this:

   String text = "17:27 - 13:10(+1 day) Trip duration 13h43 New York (JFK) 1 x transfer Includes travel by bus Maastricht (ZYT) Lowest fare USD 862 View flight details";   
   String departTime = text.substring(0, 5);
   String arrivalTime = text.substring(8, 13);
   String depart = text.substring(42, 56);
   String arrive = text.substring(93, 109);
   String transfers = text.substring(57, 58);
   String price = text.substring(126, 129);
   System.out.println(" departTime: "+departTime+"\n arrivalTime: "+arrivalTime+"\n depart: "+depart+"\n arrive: "+arrive+"\n transfers: "+transfers+"\n price: "+price);

But this won't work as the cases are different. Below are more text examples:

18:50 - 13:10(+1 day) Trip duration 12h20 New York (JFK) 1 x transfer Includes travel by bus Maastricht (ZYT) KL0646 Inflight services: Lowest fare USD 862 View flight details
12:00 - 13:10(+1 day) Trip duration 19h10 New York (JFK) 2 x transfer Includes travel by bus Maastricht (ZYT) Inflight services: FromUSD 864 View flight details
12:00 - 13:10(+1 day) Trip duration 19h10 New York (JFK) 2 x transfer Includes travel by bus Maastricht (ZYT) FromUSD 864 View flight details 3 seats left at this fare
07:45 - 13:10(+1 day) Trip duration 23h25 New York (JFK) 2 x transfer Includes travel by bus Maastricht (ZYT) FromUSD 864 View flight details 1 seat left at this fare 

Jul 19, 2020 in Java by laiman
• 330 points
1,129 views

Hi, @There,

Do you want a wholesome code regarding your above query? According to your explanation are you trying to mean a code where your substring from the main string will be available?

1 answer to this question.

0 votes

Hello @laiman,

You can use IsMatch function that tests whether a text string matches a pattern that can comprise ordinary characters, predefined patterns, or a regular expression. The Match and MatchAll functions return what was matched, including sub-matches.

Syntax

IsMatch( TextPattern [, Options ] )

  • Text – Required. The text string to test.
  • Pattern – Required. The pattern to test as a text string. Concatenate predefined patterns that the Match enum defines, or provide a regular expression. Pattern must be a constant formula without any variables, data sources, or other dynamic references that change as the app runs.
  • Options – Optional. A text-string combination of MatchOptions enum values. By default, MatchOptions.Complete is used.

Imagine that your app contains a Text input control named TextInput1. The user enters values into this control to be stored in a database.

The user types Hello world into TextInput1.

ORDINARY CHARACTERS
Formula Description Result
IsMatch( TextInput1.Text, "Hello world" ) Tests whether the user's input matches, exactly, the string "Hello world". true
IsMatch( TextInput1.Text, "Good bye" ) Tests whether the user's input matches, exactly, the string "Good bye". false
IsMatch( TextInput1.Text, "hello", Contains ) Tests whether the user's input contains the word "hello" (case sensitive). false
IsMatch( TextInput1.Text, "hello", Contains & IgnoreCase ) Tests whether the user's input contains the word "hello" (case insensitive).

 
true
You can also refer to this doc-ISMatch
Hope it helps!!
Thank you!!
answered Jul 28, 2020 by Niroj
• 82,840 points

Related Questions In Java

0 votes
1 answer

How to get text from text box in Swing?

You can add a Button listener here. ...READ MORE

answered Aug 27, 2019 in Java by Jishan
1,318 views
0 votes
1 answer

Get selected text from a drop-down list (select box) using jQuery

Select elements typically have two values that ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
3,211 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,640 points
3,054 views
0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
2,682 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
4,695 views
0 votes
1 answer

Encode String to UTF-8

String objects in Java use the UTF-16 ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
1,394 views
0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,920 points
5,717 views
0 votes
1 answer

Java URL encoding of query string parameters

I would not use URLEncoder. Besides being incorrectly ...READ MORE

answered Jun 1, 2018 in Java by Rishabh
• 3,620 points
17,557 views
0 votes
1 answer

How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,840 points
2,026 views
0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,840 points
770 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP