基于变量值的字符串替换

时间:2017-08-02 19:49:40

标签: java string replace

我有以下字符串:{"array 1":[ .... 我想删除[之前的所有内容。

为此我使用:.replace("{\"array 1\":", "");并且效果很好。

但是,我有几个数组,所以我想根据保存数组名称的变量进行替换。

例如:

 String arr_name = "array 1";
 ....replace('{\"arr_name\":", "");

是否可以使用变量键替换字符串?

编辑:

我最终添加了另一个元素来解析JSON中删除其名称的数组。

谢谢大家的快速评论和建议。

2 个答案:

答案 0 :(得分:0)

你可以做一个字符串格式,这是一个例子:

$(this).closest('.section').find('.popup-overlay').removeClass('popup-overlay-show');
$(this).closest('.section').find('.popup-overlay').addClass('popup-overlay-show');

答案 1 :(得分:0)

只需使用一个子字符串,从索引import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class Main { static SocketChannel channel; public static void main(String[] args) throws IOException { System.out.println("Listening for connections on : 8888"); //8888 ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.bind(new InetSocketAddress(8888)); channel = serverChannel.accept(); System.out.println("Connected..."); channel.setOption(StandardSocketOptions.TCP_NODELAY, true); channel.configureBlocking(false); ReceiveFromOMS receivefromOMS; SendToExchange sendExchange; receivefromOMS = new ReceiveFromOMS(); sendExchange = new SendToExchange(); Thread t1 = new Thread(receivefromOMS); Thread t2 = new Thread(sendExchange); t1.start(); t2.start(); } } class ReceiveFromOMS extends Thread{ public static SocketChannel channel; static ByteBuffer buffer = ByteBuffer.allocate(1024); static ServerSocketChannel serverChannel ; public static int ReceiveFromOMSPort; BlockingQueue<String> fromOMSqueue = new LinkedBlockingQueue<>(30); @Override public void run(){ while(true){ try { receiveFromOMS(); } catch (InterruptedException ex) { System.err.println(ex.getMessage()); try { Thread.sleep(5000); } catch (InterruptedException ex1) { } } } } public void receiveFromOMS() throws InterruptedException{ try { int numRead = -1; numRead = channel.read(buffer); while(numRead==0){ numRead = channel.read(buffer); } if (numRead == -1) { Socket socket = channel.socket(); SocketAddress remoteAddr = socket.getRemoteSocketAddress(); System.out.println("Connection closed by client: " + remoteAddr); channel.close(); return; } byte[] data = new byte[numRead]; System.arraycopy(buffer.array(), 0, data, 0, numRead); fromOMSqueue.add(new String(data)); String msg = fromOMSqueue.poll(); System.out.println("OutGoing To Exchange>> " + msg); SendToExchange.sendToEchange(msg); buffer.flip(); buffer.clear(); } catch (IOException ex) { System.err.println(ex.getMessage()); Thread.sleep(5000); } } } class SendToExchange extends Thread{ static SocketChannel channel; static ByteBuffer bb = ByteBuffer.allocateDirect(1024); static Charset charset = Charset.forName("UTF-8"); public byte[] data; public static String message; @Override public void run(){ try { while(true){ receive(); Thread.sleep(100); } } catch (IOException | InterruptedException ex) { System.err.println(ex.getMessage()); try { Thread.sleep(5000); } catch (InterruptedException ex1) {} } } public static void sendToEchange(String msg){ try { bb = stringToByteBuffer(msg, charset); channel.write(bb); } catch (IOException ex) { System.err.println(ex.getMessage()); } } public void receive() throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int numRead = -1; numRead = channel.read(buffer); while (numRead == 0) { numRead = channel.read(buffer); } if (numRead == -1) { Socket socket = channel.socket(); SocketAddress remoteAddr = socket.getRemoteSocketAddress(); System.out.println("Connection closed by Exchange: " + remoteAddr); channel.close(); return; } buffer.flip(); data = new byte[numRead]; buffer.get(data); message = new String(data); System.out.println("Incoming from Exchange>> " + message); buffer.clear(); } public static ByteBuffer stringToByteBuffer(String msg, Charset charset){ return ByteBuffer.wrap(msg.getBytes(charset)); } } 开始。

[

这会给 String input = "{\"array 1\":[key:value..."; String result = input.substring(input.indexOf('['));

相关问题