Detect parts of a string

Asked

Viewed 60 times

2

Hi, this is my first time in stackoverflow, and I’m having a hard time, I need to know how I can detect parts of a string, example:

String: "Definir a altura de %s para %d"

and I want the information %s and %d

Example:

"Definir a altura de objeto para 500"

Exit:

Saída: "objeto", 500

Is there any way to get that? NOTE: the output does not need to be necessarily in some kind of data, it can be output in 2 variables, 1 array or anything.

NOTE: Javascript Language.

Thank you.

  • You want to search a given word within a sentence and capture it?

  • Actually no, I need information that I don’t have, which can be varied, similar to a formatted entry

  • the information will always be in the same place? If it is, you can create a function and pass two parameters, thus filling them in place of the string and then returning the same.

  • will be in the same place, but I get the full string from the user in this situation: User: "Set the object height to 500" Code returns: "object", 500

  • It’ll always be that same phrase?

  • in this situation yes, but I will put more situations applying the same logic

Show 1 more comment

3 answers

5

I will make a solution safer using regular expressions, because if "object" contains spaces, using split can generate errors.

var frase = "Definir a altura de objeto para 500";
var objeto = frase.replace(/^Definir a altura de (.*) para (.*)$/, '$1');
var numero = frase.replace(/^Definir a altura de (.*) para (.*)$/, '$2');

console.log(objeto, numero);

  • great method, safer than mine. I like solutions with regex, although not understand much. + 1

2


If the phrase is always the same as you said, you can do it this way:

let phrase = "Definir a altura de cadeira para 500";

//quebra a frase no primeiro "de"
let part1 = phrase.split(' de '); 
//quebra a frase no primeiro "para"
let part2 = part1[1].split(' para ')

console.log(part2[0])
console.log(part2[1])

If you want something more dynamic, you can create a function where the first parameter will be the phrase itself and the last two will be the delimiters to perform the phrase break.

Example:

function getPhrase(phrase, param1, param2) {
    let phrases = phrase;    

    //quebra a frase no primeiro "de"
    let part1 = phrases.split(" "+param1+" "); 
    //quebra a frase no primeiro "para"
    let part2 = part1[1].split(" "+param2+" ")

    //console.log(part2[0])
    //console.log(part2[1])
    
    let result = [ {'1': part2[0].trim(), '2': part2[1].trim() }];
    
    return result;

}


$(document).ready(function(){
   let obj = getPhrase('Definir a altura de cadeira para 500', 'de', 'para');
   console.log(obj)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Remembering that I made the examples based on your information and that the code can easily be adapted.

  • Thank you very much! It worked perfectly :D

  • Not at all, friend! :)

  • But if you’re breaking the phrase in "from," what happens if the phrase is something like "Set the chair height to 500"?

  • @Andre, well noted. I edited the reply, thank you.

1

You can use the split function, sending an empty space as a separator, returning an array.

Ex:

const frase = 'Definir a altura de objeto para 500';
frase.split(' '); 
// [ 'Definir', 'a', 'altura', 'de', 'objeto', 'para', '500' ]

As an empty space was used as a separator, an array will be returned with all the words of the string. You can find them by the index.

frase[4];
// objeto

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.