String Format
package com.minte9.basics.strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringFormat {
public static void main(String[] args) {
String a = "0" + String.format("%6s", "123");
String b = String.format("%6s", "123").replace(" ", "0");
String c = String.format("%-6s", "123").replace(" ", "0");
System.out.println(a);
System.out.println(b);
System.out.println(c);
List<Integer> N = new ArrayList<>();
List<String> S = new ArrayList<>();
N = Arrays.asList(100, 65, 50);
S = Arrays.asList("java", "cpp", "python");
for (int i=0; i<N.size(); i++) {
System.out.print(
String.format("%-15s", S.get(i)) +
String.format("%03d", N.get(i)) +
"\n"
);
}
}
}
Multilines
package com.minte9.basics.strings;
public class Multilines {
public static void main(String[] args) {
String a =
+ "AAA "
+ "BBB"
;
String b = String.join("\n",
"CCC",
"DDD"
);
System.out.println(a);
System.out.println(b);
}
}
Converting / Integers
Conveting Strings to Integers and vice versa.
T
package com.minte9.basics.strings;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import org.junit.Test;
public class StringConvertionTest {
Integer a = Integer.valueOf("1");
Float b = Float.valueOf("2.22");
int x = 11;
@Test public void toNumberTest() {
assertEquals(a, 1, 0);
assertNotEquals(a, "1");
assertEquals(b, 2.23, 0.1);
assertNotEquals(b, 2.23, 0);
}
@Test public void toStringTest() {
assertEquals("11", + x);
assertEquals("11", + String.valueOf(x));
assertEquals("11", + Integer.toString(x));
}
}
Json
Compare to XML, JSON is much smaller in size.
T
package com.minte9.basics.strings;
import static org.junit.Assert.assertEquals;
import org.json.JSONObject;
import org.junit.Test;
public class JsonTest {
@Test public void json() {
String s = "{message:{type:feed,timestamp:1},error:0}";
JSONObject obj = new JSONObject(s);
assertEquals("feed", obj.getJSONObject("message").getString("type"));
assertEquals(0, obj.getInt("error"));
}
}
Translate
Custom formats can be easily transtated to Json.
package com.minte9.basics.strings;
import org.json.JSONObject;
public class Translate {
public static void main(String[] args) {
String s = "{message={type=feed|timestamp=1},error=0}";
JSONObject obj;
s = translate(s);
obj = new JSONObject(s);
System.out.println(
obj.getJSONObject("message").getString("type")
);
System.out.println(
obj.getInt("error")
);
}
public static String translate(String s) {
s = s.replaceAll("([^={}|]+)=", "$1=");
s = s.replaceAll("=([^={}|]+)", "=$1");
s = s.replace("|", ",").replace("=", ":");
return s;
}
}
Loop
To parse json array, use for-each loop with getJSONArray() method.
package com.minte9.basics.strings;
import org.json.JSONArray;
import org.json.JSONObject;
public class Loop {
public static void main(String[] args) {
String str = "{"
+ "orders: ["
+ "{sym:goog,amount:100,error:1},"
+ "{sym:msft,amount:200,error:0}"
+ "], error:0" +
"}";
JSONObject obj = new JSONObject(str);
JSONArray orders = obj.getJSONArray("orders");
for(Object item : orders) {
JSONObject order = (JSONObject) item;
System.out.println(
+ order.getString("sym")
+ ":"
+ order.getInt("error")
);
}
}
}