How to remove a part of a string?

Asked

Viewed 3,257 times

-2

I have a text saved in a string and need to copy a part of that text to find a certain word in it.

Ex. I need to remove my name and my age from this text, I know they always come after : and in quotes.

String texto = "Meu nome: "+ "Mariane Teixeira" + ", Minha idade: "+ "22";

//resposta
String nome = "Mariane Teixeira"
String idade = "22"

I can use as FLAG os : or "" or the word "name" and "age" and copy what comes after them, but I don’t know how to do it using java code.

  • 1

    Tried to do something already?

  • This string you created has some syntax error!

  • @Articuno no... I’m with logic in mind but I don’t know how to pass it to code. I don’t know very well the string libraries

  • @white string is "My name: "Mariane Teixeira", My age: "22"

  • 1

    @Mariteixeira even so, this does not compile. You have to put exactly as it is in your code, if it is in your code. Try to describe your problem better.

  • It is no longer interesting to use the format JSON?

  • @White string text = "My name: "+ "Mariane Teixeira" + ", My age: "+ "22";

  • @Mariteixeira would be interesting you leave in your question, exactly as it is in your code. If you want, you can even edit and fix it there.

  • If you are already contacting, why do you want to decontaminate? hehe =)

  • 1

    If there is a pattern use regular expressions

  • What part exactly do you need to copy from this text? Just remove all occurrences of a given string? Or just copy what comes before, or just what comes after?

  • just what comes after the two points. are various information, name: ........ , age: ........... , address: .......... need that information

  • Do the following. How is your file? Does it have a standard format or is it irregular? What attributes exist in your file? Or do you only need the ones you described (name, age, address)? For a correct and necessary answer you edit the question and put a part of your file in it.

Show 8 more comments

2 answers

3


You can use a regular expression to extract the values from your String:

([^,\:]+)\:([^\,]+)

This regular expression will look for any sequence of 1 or more characters that are not , nor : (([^,\:]+)) followed by : (\:) which has any sequence of one or more characters which do not contain , (([^\,]+)). In the Java escape the regular expression special characters with two bars (\\). So I came up with the following method that turns your String in a Map:

public static Map<String, String> separar(String texto) {
  String regex = "([^,\\:]+)\\:([^\\,]+)";
  Map<String, String> resultado = new HashMap<>();

  Pattern parte = Pattern.compile(regex);
  Matcher matcher = parte.matcher(texto);

  while (matcher.find()) {
    String chave = matcher.group(1);
    String valor = matcher.group(2);

    resultado.put(chave.trim(), valor.trim());
  }

  return resultado;
}

You can test the method with your String as follows:

public static void main(String[] args) {
  String texto = "Meu nome: Mariane Teixeira, Minha idade: 22";

  Map<String, String> valores = separar(texto);

  valores.forEach((chave, valor) -> System.out.println(chave + ": " + valor));
}

The amount printed on console would be the following:

Meu nome: Mariane Teixeira
Minha idade: 22

Some remarks:

  • Your String is very similar to the notation of a JSON, although it does not represent a JSON valid. Therefore, consider using it.

  • In your example you used the code "Meu nome: "+ "Mariane Teixeira" + ", Minha idade: "+ "22" to generate the String. The result would be "Meu nome: Mariane Teixeira, Minha idade: 22". If you want to quote the values the assignment should be "Meu nome: \"Mariane Teixeira\", Minha idade: \"22\"".

1

A very lazy way to solve:

String text = "Meu nome: \"Mariane Teixeira\", Minha idade: \"22\"";
String [] textArray = text.split("\"");

System.out.println(Arrays.toString(textArray));

System.out.println("Meu nome: " + textArray[1]);
System.out.println("Minha idade: " + textArray[3]);

Browser other questions tagged

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