处理拆分(服务器)

时间:2016-06-20 17:55:08

标签: string split server client processing

我正在进行2人游戏,当我从服务器获取信息时,它的格式为" topic; arg1; arg2"所以,如果我发送职位,那就是#P; PlayerPos; x; y"。 然后我使用split方法和#34 ;;"。 但后来......我甚至试图在屏幕上写下它" PlayerPos"写的是对的,但是不能通过if获得。

这是我在服务器上发送信息的方式:

 
server.write("PlayerPos;"+player1.x+";"+player1.y);

我如何在客户端接受它:

String Get=client.readString();
  String [] Getted = split(Get, ';');
  fill(0);
  text(Get,20,20);
  text(Getted[0],20,40);
if(Getted[0]=="PlayerPos"){
  text("HERE",20,100);
  player1.x=parseInt(Getted[1]);
  player1.x=parseInt(Getted[2]);
}
它写了我" PlayerPos; 200; 200"在屏幕上,甚至" PlayerPos"在它下面。但它从未写过#34; HERE"它永远不会成为if。 我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

比较==值时,请勿使用String。请改用equals()功能:

 
if(Getted[0].equals("PlayerPos")){

来自the Processing reference

  

要比较两个字符串的内容,请使用equals()方法,而不是if (a.equals(b)),而不是if (a == b)StringObject,因此将它们与==运算符进行比较只会比较Strings是否存储在同一个内存位置。使用equals()方法将确保比较实际内容。 (troubleshooting reference有更长的解释。)