locate string numbers

0

I'm trying to do the following: I have a string type this:

String req="Intel Core i7-8700K 3.7 Ghz BOX";
String req2="Procesador AMD Ryzen 5 2600X 4.2 Ghz";

What I want is to extract the part that says "3.7 Ghz" and "4.2 Ghz" in a string. These are a couple of examples, so it does not help me to count characters, since as you can see, they change their position. What is always maintained is that I want to get the same number of characters before Ghz ... Can you guide me on how to do it?

The only thing I have is this, but it does not work for me in this case ...

int fin = REQprocesador.indexOf("GHz", inicio + 1);

Thanks!

    
asked by aidamf 22.05.2018 в 00:13
source

3 answers

1

In Java with the Pattern and Matcher class we can do the validations and obtain the capture groups using the group method of the Matcher class:

private static final Pattern p = Pattern.compile("\d\.\d GHZ");
    public static void main(String[] args) {
        Matcher m = p.matcher("Intel Core i7-8700K 3.7 Ghz BOX");
        if (m.find()) {
            System.out.println(m.group(0)); 
            System.out.println(m.group(1)); 
            System.out.println(m.group(2)); 
            System.out.println(m.group(3)); 
        }
    }

This simplifies extracting the values of a string, in a less fragile way and easier to read later the source code than using the split function by one or more certain characters, this is a fairly simple string if the pattern is more complex the code using split can be significantly complicated. The Matcher class contains more useful methods, for example, with the start and end methods it is possible to know the initial and final position of each capture group

    
answered by 22.05.2018 в 00:38
0

Try the following code:

public static void main(String[] args) {
    String req="Intel Core i7-8700K 3.7 Ghz BOX";
    String req2="Procesador AMD Ryzen 5 2600X 4.2 Ghz";

    System.out.println("Core 1: "+getCore(req));
    System.out.println("Core 2: "+getCore(req2));

}

private static String getCore(String req){
    //Se busca en el string el patrón Ghz
    int index = req.indexOf("Ghz");
    //Se realiza el substring de la palabra pasando como parametro la resta de 
    //4 a la variable anterior (son las posiciones delante de Ghz) y sumandole
    //3 (la longitud de Ghz).
    return req.substring(index-4, index+3);
}

Console output:

Core 1: 3.7 Ghz
Core 2: 4.2 Ghz
    
answered by 22.05.2018 в 00:37
0

I do not know exactly how it would be done in java but I'm going to leave it in javascript and what you would have to apply is the same logic in java ..

  • Identify the string
  • You separate it by space
  • you search for the item that contains 'Ghz'
  • And finally joins it with the previous element that contains the amount
  • var cadena = "Intel Core i7-8700K 3.7 Ghz BOX";
    var elementos = cadena.split(" ");
    for(a = 0; a <elementos.length; a++){
    if(elementos[a] == 'Ghz'){
    alert(elementos[a-1]+' '+elementos[a])
    }
    }

    Hopefully, what you want and help you with what you need.

        
    answered by 22.05.2018 в 00:54