我正在将一个字符串解析为由'&'
字符的第一个外观划分的2个部分。例如:如果字符串为555&hello &world
,则分区将为555
和hello &world
。我使用了以下代码:
String[] numberAndMessage=currentMessage.split("(\\&)",2);
System.out.println(numberAndMessage[1]+storedMessage.message+"end");
所以,输入:555&hello &world
我预期的输出是:hello &worldend
但相反它给了:hello &world
第二个分区的结束字符可能有任何问题。 但是问题是什么?
代码:
UDPServer.java:
import java.net.*;
public class UDPServer extends Thread{
public void run(){
try{
DatagramSocket serverSocket = new DatagramSocket(9999);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String currentMessage = new String( receivePacket.getData());
System.out.println("RECEIVED: " + currentMessage);
String[] numberAndMessage=currentMessage.split("(\\&)", 2);
System.out.println("noAndMessage[0]="+numberAndMessage[0]+";noAndMessage[1]="+numberAndMessage[1]);
System.out.println("numberAndMessage[1]= "+numberAndMessage[1]+"end");
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception
{
UDPServer server=new UDPServer();
server.start();
}
}
UDPClient.java:
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
UDPClient.java输入:
555&hello world
UDPServer.java的输出:
RECEIVED: 555&hello world
noAndMessage[0]=555;noAndMessage[1]=hello world
numberAndMessage[1]= hello world
答案 0 :(得分:0)
我不知道storedMessage.message
。但是下面的代码对预期输出很有用。
String currentMessage="555&hello &world";
String[] numberAndMessage=currentMessage.split("(\\&)",2);
System.out.println(numberAndMessage[1]+"end");
在这里你会得到OUtPUT:
hello &worldend
答案 1 :(得分:-1)
试试这个
public class Test {
public static void main(String[] args) {
String currentMessage = "555&hello &world";
String[] numberAndMessage = currentMessage.split("(\\&)", 2);
System.out.println(numberAndMessage[1] + "end");
}
}
输出
hello &worldend
您的完整代码我得到了以下结果
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer extends Thread{
public void run(){
try{
DatagramSocket serverSocket = new DatagramSocket(9999);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
String a = "555&hello world";
receiveData = a.getBytes();
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
//serverSocket.receive(receivePacket);
String currentMessage = new String( receivePacket.getData());
System.out.println("RECEIVED: " + currentMessage);
String[] numberAndMessage=currentMessage.split("(\\&)", 2);
System.out.println("noAndMessage[0]="+numberAndMessage[0]+";noAndMessage[1]="+numberAndMessage[1]);
System.out.println("numberAndMessage[1]= "+numberAndMessage[1]+"end");
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception
{
UDPServer server=new UDPServer();
server.start();
}
}
输出
RECEIVED: 555&hello world
noAndMessage[0]=555;noAndMessage[1]=hello world
numberAndMessage[1]= hello worldend