String处理

 

 

1 获取字符串token的性能比较,输出为250,31。StringTokenizer远胜于split的方式。

 

		for (int i = 0; i < 1000; i++) {
			new String();
		}
		String string = "this is a test";
		long start = System.currentTimeMillis();

		for (int i = 0; i < 100000; i++) {
			String[] strings = string.split("\\s");
			String temp = strings[1];
			// System.out.println(strings[1]);

		}
		System.out.println(System.currentTimeMillis() - start);
		start = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			StringTokenizer st = new StringTokenizer(string);
			st.nextToken();
			String temp = st.nextToken();
			// System.out.println(st.nextToken());

		}
		System.out.println(System.currentTimeMillis() - start);
	

 

2 json,object,字符串相互转换

 

 

		UserDO userDO = new UserDO();
		userDO.setAddress("{:[][];}adfadf,,}");
		userDO.setAge(123123);
		JSONObject jsonObject = JSONObject.fromObject(userDO);
		String temp = jsonObject.toString();
		System.out.println(temp);
		jsonObject.put("jsonkey", "jsonvalue");
		temp = jsonObject.toString();
		System.out.println(temp);
		JSONObject.fromObject(temp).get("address");
		System.out.println(temp);
	
 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(String)