[RESOLVIDO] Pegar somente aquela parte da String

17 respostas
SirDominque

Qual a lógica que tenho que usar pra dividir uma String?

Tenho uma String Muito Grande e eu preciso pegar exatamente essa parte “team_t tickets_t red 431 blue 363”

A String vem ± Do jeito que esta em baixo :
Se tiver mais jogadores ela aumenta…

ahostname -[AAAAA]-AAA | Allmaps hostport 1234 mapname AAA 1968 gametype conquest numplayers 35 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods all_active_mods bfvietnam game_id_name BFVietnam #player_ score_ deaths_ ping_ team_ kills_ NOOB ZERO 0 0 164 1 0 Player 4 10 292 1 4 team_t tickets_t red 431 blue 363
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

17 Respostas

mmaico

Olá,

Você pode usar uma regex para encontrar o texto, segue um exemplo:

String patternStr = "\\b team_t tickets_t red 431 blue 363\\b";
		
                Pattern pattern = Pattern.compile(patternStr);
		               Matcher matcher = pattern.matcher("Aqui vai o texto onde você irá procurar a string acima");
             
                if (matcher.find()) {
                      System.out.println(matcher.group()); 
                }

Abraço!

SirDominque

Hmm, mas nem sempre a quantidade de tickets vai ser a mesma…

Tipo
team_t tickets_t red 431 blue 363
o
team_t tickets_t red 400 blue 300

Na verdade eu só preciso disso :

red 400
blue 300

DA pra usar regex mesmo sabendo que os numeros podem dar diferente?

lina

Oi,

Esse texto sempre estará no final? Ou ele pode se intrometer e ir para o meio do texto?

Tchauzin!

SirDominque

lina:
Oi,

Esse texto sempre estará no final? Ou ele pode se intrometer e ir para o meio do texto?

Tchauzin!

Acredito que Sempre no Final;

lina
Andre Lopes:
lina:
Oi,

Esse texto sempre estará no final? Ou ele pode se intrometer e ir para o meio do texto?

Tchauzin!

Acredito que Sempre no Final;

Oi,

Você poderá usar indexOf

String
texto			=	"hostname -[AAAAA]-AAA | Allmaps hostport 1234 mapname AAA 1968 gametype conquest numplayers 35 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods all_active_mods bfvietnam game_id_name BFVietnam #player_ score_ deaths_ ping_ team_ kills_ NOOB ZERO 0 0 164 1 0 Player 4 10 292 1 4 team_t tickets_t red 431 blue 363";
		
String
pedacos = texto.substring(texto.indexOf("team_t"),texto.length());
		
System.out.println(pedacos);

Tchauzin!

SirDominque
lina:
Andre Lopes:
lina:
Oi,

Esse texto sempre estará no final? Ou ele pode se intrometer e ir para o meio do texto?

Tchauzin!

Acredito que Sempre no Final;

Oi,

Você poderá usar indexOf

String
texto			=	"hostname -[AAAAA]-AAA | Allmaps hostport 1234 mapname AAA 1968 gametype conquest numplayers 35 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods all_active_mods bfvietnam game_id_name BFVietnam #player_ score_ deaths_ ping_ team_ kills_ NOOB ZERO 0 0 164 1 0 Player 4 10 292 1 4 team_t tickets_t red 431 blue 363";
		
String
pedacos = texto.substring(texto.indexOf("team_t"),texto.length());
		
System.out.println(pedacos);

Tchauzin!

Valeu, ele
retornou isso aqui :

team_t tickets_t red 431 blue 363

lina

Oi,

O que você pode fazer é usar o indexOf para iniciar da tag tickets_t + 9.

Tchauzin!

mmaico

Olá André,

Dá sim, faz o seguinte:

String patternStr = "(blue [0-9]{0,3}) | (red [0-9]{0,3})";
      Pattern pattern = Pattern.compile(patternStr);   
      Matcher matcher = pattern.matcher("Aqui vai o texto onde você irá procurar a string acima");  
  
      while (matcher.find()) {  
           System.out.println(matcher.group());   
      }

E expressão diz que eu quero blue ou red seguido de um conjunto de 3 numeros que pode ir de zero a nove, ou seja ele casa com
red 0 a red 999 ou blue 0 a blue 999.

Até mais!

SirDominque
mmaico:
Olá André, Dá sim, faz o seguinte:
String patternStr = "(blue [0-9]{0,3}) | (red [0-9]{0,3})";
      Pattern pattern = Pattern.compile(patternStr);   
      Matcher matcher = pattern.matcher("Aqui vai o texto onde você irá procurar a string acima");  
  
      while (matcher.find()) {  
           System.out.println(matcher.group());   
      }

E expressão diz que eu quero blue ou red seguido de um conjunto de 3 numeros que pode ir de zero a nove, ou seja ele casa com
red 0 a red 999 ou blue 0 a blue 999.

Até mais!

Valeu, mas..
Nao Imprimiu nada.

public int queryTeamOneTickets() {
        int tickets = 1000;
        //
        ServerInfo serverInfo = QueriEd.serverQuery(localPort, gameType, ipStr, port);
        String fullResponse = serverInfo.getFullResponse();
        //
       // String pedacos = fullResponse.substring(fullResponse.indexOf("team_t"), fullResponse.length());
        //System.out.println(pedacos);
        //
        //

        String patternStr = "(blue [0-9]{0,3}) | (red [0-9]{0,3})";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(fullResponse);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }


        //
        return tickets;
    }
lina

Oi,

Tente usar a expressão regular dessa forma:

String patternStr = "(red [0-9]{3,})|(blue [0-9]{3,})";

Tchauzin!

SirDominque

lina:
Oi,

Tente usar a expressão regular dessa forma:

String patternStr = "(red [0-9]{3,})|(blue [0-9]{3,})";

Tchauzin!

O fullresponse ta chegando assim :

arun:
ahostname -[AAAAA]-AAA | Allmaps hostport 11111 mapname OPERATION CEDAR FALLS gametype conquest numplayers 23 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods all_active_mods bfvietnam game_id_name BFVietnam player_ score_ deaths_ ping_ team_ kills_ «ßÔß»-ßêR$ërKêR-[CLF] 3 2 129 1 3 Mr President 0 0 201 1 0 —>haschmaker<—(CH) 0 0 144 2 0 david 11 3 124 2 1 UZBEK 5 2 184 1 1 major larry thorne 4 0 167 2 4 459er 2 5 69 1 2 Cpt. Jean Danjou 3 4 123 1 1 Domestabot 4 3 316 2 4 ~SiR~ Deltoya 17 4 257 2 9 beznika 4 3 234 2 1 Miscreant 0 0 65 1 0 ;,-:Rommel:-,; 0 0 221 2 0 BB 7 2 163 2 0 Player_0 2 3 134 2 2 MIG 0 0 647 2 0 joorub 4 2 128 1 1 Forcelex 9 6 128 1 2 DEVGRU 6 4 66 2 3 Player_1 1 2 175 1 1 MorsDuuD 7 2 171 1 4 |GF| blueberry 0 0 62 1 0 «at(o)mic»>poopie 0 0 0 1 0 team_t tickets_t red 410 blue 402

CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

Mas ainda nao deu certo;;;

lina

Oi,

Eu fiz esse teste aqui e deu certo:

String
texto			=	" hostname -[AAAAA]-AAA | Allmaps hostport 11111 mapname OPERATION CEDAR FALLS gametype conquest numplayers 23 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill  reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods  all_active_mods bfvietnam game_id_name BFVietnam   player_ score_ deaths_ ping_ team_ kills_  «ßÔß»-ßêR$ërKêR-[CLF] 3 2 129 1 3 Mr President 0 0 201 1 0 --->haschmaker<---(CH) 0 0 144 2 0 david 11 3 124 2 1 UZBEK 5 2 184 1 1 major larry thorne 4 0 167 2 4 459er 2 5 69 1 2 Cpt. Jean Danjou 3 4 123 1 1 Domestabot 4 3 316 2 4 ~SiR~ Deltoya 17 4 257 2 9 beznika 4 3 234 2 1 Miscreant 0 0 65 1 0 ;,_-:Rommel:-_,; 0 0 221 2 0 BB 7 2 163 2 0 Player_0 2 3 134 2 2 MIG 0 0 647 2 0 joorub 4 2 128 1 1 Forcelex 9 6 128 1 2 DEVGRU 6 4 66 2 3 Player_1 1 2 175 1 1 MorsDuuD 7 2 171 1 4 |GF| blueberry 0 0 62 1 0 «at(o)mic»>poopie 0 0 0 1 0 team_t tickets_t  red 410 blue 402 ";
		
String patternStr = "(red [0-9]{0,})|(blue [0-9]{0,})";
Pattern pattern = Pattern.compile(patternStr);   
Matcher matcher = pattern.matcher(texto);  
	  
while (matcher.find()) {  
   System.out.println(matcher.group());   
}  
		
String
pedacos			=	texto.substring(texto.indexOf("tickets_t")+11,texto.length());
		
System.out.println(pedacos);

Tchauzin!

SirDominque
lina:
Oi,

Eu fiz esse teste aqui e deu certo:

String
texto			=	" hostname -[AAAAA]-AAA | Allmaps hostport 11111 mapname OPERATION CEDAR FALLS gametype conquest numplayers 23 maxplayers 52 gamemode openplaying password 0 gamever v1.2 dedicated 2 status 4 game_id bfvietnam map_id BFVietnam sv_punkbuster 0 timelimit unlimited number_of_rounds 1 spawn_wave_time 5s spawn_delay 5s soldier_friendly_fire 0% vehicle_friendly_fire 0% game_start_delay 15s ticket_ratio 150% allow_nose_cam yes external_view on us_team_ratio 1 nva_team_ratio 1 bandwidth_choke_limit 0 free_camera off auto_balance_teams on name_tag_distance 100 name_tag_distance_scope 300 kickback 0% kickback_on_splash 0% soldier_friendly_fire_on_splash 0% vehicle_friendly_fire_on_splash 0% cpu 2493 bot_skill  reservedslots 0 spectatorsallowed 1 spectatorvoting 1 spectatorswitchtime 10s active_mods  all_active_mods bfvietnam game_id_name BFVietnam   player_ score_ deaths_ ping_ team_ kills_  «ßÔß»-ßêR$ërKêR-[CLF] 3 2 129 1 3 Mr President 0 0 201 1 0 --->haschmaker<---(CH) 0 0 144 2 0 david 11 3 124 2 1 UZBEK 5 2 184 1 1 major larry thorne 4 0 167 2 4 459er 2 5 69 1 2 Cpt. Jean Danjou 3 4 123 1 1 Domestabot 4 3 316 2 4 ~SiR~ Deltoya 17 4 257 2 9 beznika 4 3 234 2 1 Miscreant 0 0 65 1 0 ;,_-:Rommel:-_,; 0 0 221 2 0 BB 7 2 163 2 0 Player_0 2 3 134 2 2 MIG 0 0 647 2 0 joorub 4 2 128 1 1 Forcelex 9 6 128 1 2 DEVGRU 6 4 66 2 3 Player_1 1 2 175 1 1 MorsDuuD 7 2 171 1 4 |GF| blueberry 0 0 62 1 0 «at(o)mic»>poopie 0 0 0 1 0 team_t tickets_t  red 410 blue 402 ";
		
String patternStr = "(red [0-9]{0,})|(blue [0-9]{0,})";
Pattern pattern = Pattern.compile(patternStr);   
Matcher matcher = pattern.matcher(texto);  
	  
while (matcher.find()) {  
   System.out.println(matcher.group());   
}  
		
String
pedacos			=	texto.substring(texto.indexOf("tickets_t")+11,texto.length());
		
System.out.println(pedacos);

Tchauzin!

Valeu, deu certoooooo

Valeu gente :D

mmaico

Ei man,

Tenta esse pattern, os espaço que estava ferrando eu ajustei aqui e testei.

(red\s[0-9]{0,})|(blue\s[0-9]{0,})

té mais!

lina

Oi,

Favor colocar o tópico como resolvido.

Tchauzin!

SirDominque
NA verdade estou com outro problema agora...
public int queryTeamRedTickets(String fullResponse) {
        int nvaTickets = 000;
        //
      
        String pedacos = fullResponse.substring(fullResponse.indexOf("tickets_t") + 11,fullResponse.length());
        String t1 = pedacos.substring(4,7);
        System.out.println("" + t1);
        nvaTickets = Integer.parseInt(t1);
        //
        return nvaTickets;
    }
    
    public int queryTeamBlueTickets(String fullResponse)
    {
        int usaTickets = 000;
        //
        String pedacos = fullResponse.substring(fullResponse.indexOf("tickets_t") + 11,fullResponse.length());
        String t2 = pedacos.substring(13,16);
        System.out.println(t2);
        //
        usaTickets = Integer.parseInt(t2);
        return usaTickets;
    }

Eu envio a String fullResponse que é aquela String cheia de informações, e pego somente os tickets... Porém,
Quando is tickets estao abaixo de 100 , ou seja, 99,98;97;96;95;94 -> 0 , ele da erro no Integer.parseInt

O que faço ?

mmaico

Provavelmente tem um espaço, da um “stringComNumero”.trim() e depois converte!

até!

Criado 19 de novembro de 2012
Ultima resposta 20 de nov. de 2012
Respostas 17
Participantes 3