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\"".
Tried to do something already?
– user28595
This string you created has some syntax error!
– viana
@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
– Mari Teixeira
@white string is "My name: "Mariane Teixeira", My age: "22"
– Mari Teixeira
@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.
– viana
It is no longer interesting to use the format
JSON?– Sorack
@White string text = "My name: "+ "Mariane Teixeira" + ", My age: "+ "22";
– Mari Teixeira
@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.
– viana
If you are already contacting, why do you want to decontaminate? hehe =)
– viana
If there is a pattern use regular expressions
– R.Santos
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?
– Wilker
just what comes after the two points. are various information, name: ........ , age: ........... , address: .......... need that information
– Mari Teixeira
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.
– Wilker