0

for example I have string

"city: Berlin, year: 1990, country: Germany",

I need to find the number 1990 after substring 'year:', the goal is to print the year from the substring.

I have list of strings(from each string I need to ger year) and iteration is:

        for(int i=0;i<arr.size();i++){ 
            str_ = arr.get(i);}  

I don't know how to proceed can someone help me please?

ncica
  • 7,015
  • 1
  • 15
  • 37
  • Take a look at [split](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java). If it's already split, you'll want to keep track of what you saw last. – Gryphon Sep 03 '20 at 23:49
  • There are several ways to do this. Here is one: `System.out.println(myString.replaceAll("[^\\d+]", ""));` prints just the year to the console window. – DevilsHnd - 退職した Sep 04 '20 at 06:01

1 Answers1

0
public class YearFinder{


public static void main(String args[]) {
    String test="city: Berlin, year: 1990, country: Germany";
    int str=test.lastIndexOf("year");
     for (int i=0;i<5;i++) {
     System.out.print(test.charAt(str+i));
     }
}}

If the texts' formats are the same and you just want a print. Roughly. I'm new to java, but this would my solution. You may also put this into a method and apply it to the array of the strings you have.