为什么拆分(新的字符['\\'])不能像我想的那样工作

时间:2016-07-29 06:02:23

标签: c#

不知道为什么案例2不能 final JSONObject body = new JSONObject(); try { // Todo: populate JSON body } catch (JSONException e) { e.printStackTrace(); } StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://...", new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } } ) { @Override public byte[] getBody() throws AuthFailureError { return body.toString().getBytes(); } @Override public String getBodyContentType() { return "application/json"; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); Splitstra

b

4 个答案:

答案 0 :(得分:7)

new char['\\']是一个大小为92的字符数组。这是由于从charint的隐式转换:

int number = '\\'; // number is 92
char[] array = new char['\\']; // char array of size 92 with default values

这就像你写的那样:

var result = str.Split(new char[92]);

单个字符数组的编写方式如下:

var result = str.Split(new char[] { '\\' });

答案 1 :(得分:3)

这是因为new char['\\']创建了(int)'\\'长度的字符数组。如果你想要一个具有给定值的数组,如果你认为它足够可读,你可以new char[] { '\\' }甚至new[] { '\\' }

答案 2 :(得分:0)

这样做:

string str = @"a\b";

String[] result = str.Split('\\'); // work (case 1)

答案 3 :(得分:0)

new char[]是一个数组。使用

var result = str.Split(new char[] { '\\' });