Исходная строка с пробелами
String [] array = {" Beer", "Old Whiskey "," Vodka "} for (int i = 0; i < array.length; i++) array[i] = array[i].trim();
Рузультат
{"Beer", "Old Whiskey", "Vodka"}
Core Java, Java EE, GWT, GXT, EXT GWT, JavaScript, CSS, HTML, jQuery, jQuery Mobile, PhoneGap, iOS, Android, GAE, Python
String [] array = {" Beer", "Old Whiskey "," Vodka "} for (int i = 0; i < array.length; i++) array[i] = array[i].trim();
{"Beer", "Old Whiskey", "Vodka"}
class DeviceInfo implements Serializable{ private String name; private transient String description; private String article; ... }
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class DeviceInfo implements Serializable { private String name; private transient String description; private String article; public DeviceInfo(String name, String description, String article) { this.name = name; this.description = description; this.article = article; } @Override public String toString() { StringBuffer sb = new StringBuffer(40); sb.append("Name : "); sb.append(this.name + "\n"); sb.append("Description : "); sb.append(this.description + "\n"); sb.append("Article : "); sb.append(this.article + "\n"); return sb.toString(); } } public class TransientTest { public static void main(String[] args) throws Exception { // создаем объект DeviceInfo deviceInfo = new DeviceInfo("Iphone 6", "Very luxury device", "A1644"); // сериализуем объект ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("store")); o.writeObject(deviceInfo); o.close(); // десериализуем объект ObjectInputStream in = new ObjectInputStream(new FileInputStream("store")); DeviceInfo deviceInfo1 = (DeviceInfo) in.readObject(); System.out.println(deviceInfo1); } }
String str = "with | great | power | comes | great | responsibility";
String[]values = str.split("\\|");
String[]values = str.split("\\|"); System.out.println(Arrays.toString(values));
values = str.split(Pattern.quote("|")); System.out.println(Arrays.toString(values));
values = Pattern.compile("\\|").split(str); System.out.println(Arrays.toString(values));
// Split using StringTokenizer class StringTokenizer tokenizer = new StringTokenizer(str, "|"); ArrayList<String> list = new ArrayList<String>(str.length()); while(tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } System.out.println(list);
import java.util.Arrays; import java.util.StringTokenizer; import java.util.regex.Pattern; public class TestA { public static void main(String[] args) { String str = "with | great | power | comes | great | responsibility"; // Simple split method String[]values = str.split("\\|"); System.out.println(Arrays.toString(values)); // Split using Pattern.quote method values = str.split(Pattern.quote("|")); System.out.println(Arrays.toString(values)); // Split using Pattern.compile method values = Pattern.compile("\\|").split(str); System.out.println(Arrays.toString(values)); // Split using StringTokenizer class StringTokenizer tokenizer = new StringTokenizer(str, "|"); ArrayList<String> list = new ArrayList<String>(str.length()); while(tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } System.out.println(list); } }