List of usage examples for java.lang StringBuilder StringBuilder
public StringBuilder(CharSequence seq)
From source file:Main.java
public static void main(String[] args) throws Exception { Class clazz = String.class; Constructor constructor = clazz.getConstructor(new Class[] { String.class }); String object = (String) constructor.newInstance(new Object[] { "Hello World!" }); System.out.println("String = " + object); constructor = clazz.getConstructor(new Class[] { StringBuilder.class }); object = (String) constructor.newInstance(new Object[] { new StringBuilder("Hello Universe!") }); System.out.println("String = " + object); }
From source file:is.java
public static void main(String[] args) { List<Integer> list = Arrays.asList(3, 2, 4, 1, 5); NavigableSet<Integer> ns = new TreeSet<Integer>(list); System.out.println("Ascending order (default): " + ns); Iterator<Integer> descendingIterator = ns.descendingIterator(); StringBuilder sb = new StringBuilder("Descending order: "); while (descendingIterator.hasNext()) { int m = descendingIterator.next(); sb.append(m + " "); }/*from w w w. ja va2 s . c o m*/ System.out.println(sb); int greatest = ns.lower(3); System.out.println("Lower of 3 = " + greatest); int smallest = ns.higher(3); System.out.println("Higher of 3 = " + smallest); }
From source file:Main.java
public static void main(String[] args) { Calendar now = Calendar.getInstance(); Date nowDate = now.getTime(); long twoHoursByMillis = 2 * HOUR; long thirtyMinutesByMillis = 30 * MINUTE; Date twoHoursAndThirtyMinutesFromNow = new Date(twoHoursByMillis + thirtyMinutesByMillis); System.out.println(String.format("now %s and later %s", nowDate, twoHoursAndThirtyMinutesFromNow)); long ms = 10304004543l; StringBuilder text = new StringBuilder(""); if (ms > DAY) { text.append(ms / DAY).append(" days "); ms %= DAY;/*from www . j a va 2 s . co m*/ } if (ms > HOUR) { text.append(ms / HOUR).append(" hours "); ms %= HOUR; } if (ms > MINUTE) { text.append(ms / MINUTE).append(" minutes "); ms %= MINUTE; } if (ms > SECOND) { text.append(ms / SECOND).append(" seconds "); ms %= SECOND; } text.append(ms + " ms"); System.out.println(text.toString()); }
From source file:Main.java
public static void main(String[] argv){ StringBuilder v = new StringBuilder("ABCD"); v.reverse(); // w w w . j ava2 s .com System.out.println(v); }
From source file:gov.nih.nci.caaersinstaller.util.CsmJaasFileCopier.java
public static void main(String args[]) throws Exception { // File csmJaasTemplateFile = new File("/Users/Moni/temp/installer/postgres.csm_jaas.config"); // File csmJassConfigFile = new File("/Users/Moni/temp/installer/csm_jaas.config"); File csmJaasTemplateFile = new File(args[0]); File csmJassConfigFile = new File(args[1]); if (csmJassConfigFile.exists()) { //append content of csmJaasTemplateFile to existing csmJaasConfigFile String csmJaasTemplateFileContent = FileUtils.readFileToString(csmJaasTemplateFile); StringBuilder stringBuilder = new StringBuilder(FileUtils.readFileToString(csmJassConfigFile)); int start = stringBuilder.indexOf("caaers {"); if (start != -1) { //If caaers context exisits then replace it. int end = stringBuilder.indexOf("};", start); end = end + 2;/* ww w.java 2 s .co m*/ stringBuilder.replace(start, end, csmJaasTemplateFileContent); } else { //if caaers context does not exist then add it stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append(csmJaasTemplateFileContent); } FileUtils.writeStringToFile(csmJassConfigFile, stringBuilder.toString()); System.out.println("Modified csm_jaas.config to add caaers context"); } else { //Create a new File with Contents of csmJaasTemplateFile FileUtils.copyFile(csmJaasTemplateFile, csmJassConfigFile); System.out.println("Created csm_jaas.config"); } }
From source file:com.cloudhopper.sxmp.demo.ParserMain.java
static public void main(String[] args) throws Exception { StringBuilder string0 = new StringBuilder(200).append("<?xml version='1.0' encoding='iso-8859-1'?>\n") .append("<!DOCTYPE malicious [\n").append(" <!ENTITY x0 \"\">\n") .append(" <!ENTITY x1 \"&x0;&x0;\">\n").append(" <!ENTITY x2 \"&x1;&x1;\">\n") .append(" <!ENTITY x3 \"&x2;&x2;\">\n").append(" <!ENTITY x4 \"&x3;&x3;\">\n") .append(" <!ENTITY x5 \"&x4;&x4;\">\n").append(" <!ENTITY x6 \"&x5;&x5;\">\n") .append(" <!ENTITY x7 \"&x6;&x6;\">\n").append(" <!ENTITY x8 \"&x7;&x7;\">\n") .append(" <!ENTITY x9 \"&x8;&x8;\">\n").append(" <!ENTITY x10 \"&x9;&x9;\">\n") .append(" <!ENTITY x11 \"&x10;&x10;\">\n").append(" <!ENTITY x12 \"&x11;&x11;\">\n") .append(" <!ENTITY x13 \"&x12;&x12;\">\n").append(" <!ENTITY x14 \"&x13;&x13;\">\n") .append(" <!ENTITY x15 \"&x14;&x14;\">\n").append(" <!ENTITY x16 \"&x15;&x15;\">\n") .append(" <!ENTITY x17 \"&x16;&x16;\">\n").append(" <!ENTITY x18 \"&x17;&x17;\">\n") .append(" <!ENTITY x19 \"&x18;&x18;\">\n").append(" <!ENTITY x20 \"&x19;&x19;\">\n") .append("]>\n").append("<operation type=\"deliver\">\n") .append("<account username=\"test\" password=\"test\"/>\n").append("<deliverRequest>\n") .append("<ticketId>&x20;</ticketId>\n").append("</deliverRequest> \n").append("</operation >\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); SxmpParser parser = new SxmpParser(SxmpParser.VERSION_1_0); //try {// w ww . j a va 2 s .c o m Operation operation = parser.parse(is); //Assert.fail(); //} catch (SxmpParsingException e) { // correct behavior //Assert.assertEquals(SxmpErrorCode.MISSING_REQUIRED_ATTRIBUTE, e.getErrorCode()); //Assert.assertThat(e.getMessage(), JUnitMatchers.containsString("The attribute [username] is required with the [account] element")); //Assert.assertNotNull(e.getOperation()); // PartialOperation partial = (PartialOperation)e.getOperation(); //Assert.assertEquals(Operation.Type.SUBMIT, partial.getType()); //} logger.debug("done!"); }
From source file:com.cloudhopper.sxmp.PostMO.java
static public void main(String[] args) throws Exception { String URL = "https://sms.twitter.com/receive/cloudhopper"; String text = "HELP"; String srcAddr = "+16504304922"; String ticketId = System.currentTimeMillis() + ""; String operatorId = "20"; StringBuilder string0 = new StringBuilder(200).append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n") .append("<operation type=\"deliver\">\n") .append(" <account username=\"customer1\" password=\"password1\"/>\n").append(" <deliverRequest>\n") .append(" <operatorId>" + operatorId + "</operatorId>\n") .append(" <sourceAddress type=\"international\">" + srcAddr + "</sourceAddress>\n") .append(" <destinationAddress type=\"network\">40404</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">" + HexUtil.toHexString(text.getBytes()) + "</text>\n") .append(" </deliverRequest>\n").append("</operation>\n").append(""); HttpClient client = new DefaultHttpClient(); client.getParams().setBooleanParameter("http.protocol.expect-continue", false); long start = System.currentTimeMillis(); // execute request try {/* w w w. j a v a 2 s . com*/ HttpPost post = new HttpPost(URL); StringEntity entity = new StringEntity(string0.toString(), "ISO-8859-1"); entity.setContentType("text/xml; charset=\"ISO-8859-1\""); post.setEntity(entity); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = client.execute(post, responseHandler); logger.debug("----------------------------------------"); logger.debug(responseBody); logger.debug("----------------------------------------"); } finally { // do nothing } long end = System.currentTimeMillis(); logger.debug("Response took " + (end - start) + " ms"); }
From source file:com.cloudhopper.sxmp.Post.java
static public void main(String[] args) throws Exception { String message = "Test With @ Character"; //String message = "Tell Twitter what you're doing!\nStd msg charges apply. Send 'stop' to quit.\nVisit twitter.com or email help@twitter.com for help."; StringBuilder string0 = new StringBuilder(200).append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"password1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>75</operatorId>\n").append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+13135551234</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">" + HexUtil.toHexString(message.getBytes("ISO-8859-1")) + "</text>\n") .append(" </submitRequest>\n").append("</operation>\n").append(""); /**//from ww w.j a va2s.co m //.append("<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML//EN\" \"../dtds/docbookx.dtd\">") //.append("<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML//EN\">") .append("<submitRequest sequenceId=\"1000\">\n") .append(" <!-- this is a comment -->\n") .append(" <account username=\"testaccount\" password=\"testpassword\"/>\n") .append(" <option />\n") .append(" <messageRequest referenceId=\"MYMESSREF\">\n") //.append(" <sourceAddress>+13135551212</sourceAddress>\n") .append(" <destinationAddress>+13135551200</destinationAddress>\n") .append(" <text><![CDATA[Hello World]]></text>\n") .append(" </messageRequest>\n") .append("</submitRequest>") .append(""); */ // Get target URL String strURL = "http://localhost:9080/api/sxmp/1.0"; // Get file to be posted //String strXMLFilename = args[1]; //File input = new File(strXMLFilename); HttpClient client = new DefaultHttpClient(); long totalStart = System.currentTimeMillis(); for (int i = 0; i < 1; i++) { long start = System.currentTimeMillis(); // execute request try { HttpPost post = new HttpPost(strURL); StringEntity entity = new StringEntity(string0.toString(), "ISO-8859-1"); entity.setContentType("text/xml; charset=\"ISO-8859-1\""); post.setEntity(entity); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = client.execute(post, responseHandler); long stop = System.currentTimeMillis(); logger.debug("----------------------------------------"); logger.debug("Response took " + (stop - start) + " ms"); logger.debug(responseBody); logger.debug("----------------------------------------"); } finally { // do nothing } } long totalEnd = System.currentTimeMillis(); logger.debug("Response took " + (totalEnd - totalStart) + " ms"); }
From source file:org.eclipse.swt.snippets.Snippet179.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 179"); shell.setLayout(new GridLayout()); final Text text = new Text(shell, SWT.BORDER); text.setText("YYYY/MM/DD"); final Calendar calendar = Calendar.getInstance(); text.addListener(SWT.Verify, new Listener() { boolean ignore; @Override//from www. j a v a 2 s . c o m public void handleEvent(Event e) { if (ignore) return; e.doit = false; StringBuilder buffer = new StringBuilder(e.text); char[] chars = new char[buffer.length()]; buffer.getChars(0, chars.length, chars, 0); if (e.character == '\b') { for (int i = e.start; i < e.end; i++) { switch (i) { case 0: /* [Y]YYY */ case 1: /* Y[Y]YY */ case 2: /* YY[Y]Y */ case 3: /* YYY[Y] */ { buffer.append('Y'); break; } case 5: /* [M]M*/ case 6: /* M[M] */ { buffer.append('M'); break; } case 8: /* [D]D */ case 9: /* D[D] */ { buffer.append('D'); break; } case 4: /* YYYY[/]MM */ case 7: /* MM[/]DD */ { buffer.append('/'); break; } default: return; } } text.setSelection(e.start, e.start + buffer.length()); ignore = true; text.insert(buffer.toString()); ignore = false; text.setSelection(e.start, e.start); return; } int start = e.start; if (start > 9) return; int index = 0; for (int i = 0; i < chars.length; i++) { if (start + index == 4 || start + index == 7) { if (chars[i] == '/') { index++; continue; } buffer.insert(index++, '/'); } if (chars[i] < '0' || '9' < chars[i]) return; if (start + index == 5 && '1' < chars[i]) return; /* [M]M */ if (start + index == 8 && '3' < chars[i]) return; /* [D]D */ index++; } String newText = buffer.toString(); int length = newText.length(); StringBuilder date = new StringBuilder(text.getText()); date.replace(e.start, e.start + length, newText); calendar.set(Calendar.YEAR, 1901); calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.DATE, 1); String yyyy = date.substring(0, 4); if (yyyy.indexOf('Y') == -1) { int year = Integer.parseInt(yyyy); calendar.set(Calendar.YEAR, year); } String mm = date.substring(5, 7); if (mm.indexOf('M') == -1) { int month = Integer.parseInt(mm) - 1; int maxMonth = calendar.getActualMaximum(Calendar.MONTH); if (0 > month || month > maxMonth) return; calendar.set(Calendar.MONTH, month); } String dd = date.substring(8, 10); if (dd.indexOf('D') == -1) { int day = Integer.parseInt(dd); int maxDay = calendar.getActualMaximum(Calendar.DATE); if (1 > day || day > maxDay) return; calendar.set(Calendar.DATE, day); } else { if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) { char firstChar = date.charAt(8); if (firstChar != 'D' && '2' < firstChar) return; } } text.setSelection(e.start, e.start + length); ignore = true; text.insert(newText); ignore = false; } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:ComputeResult.java
public static void main(String[] args) { String original = "software"; StringBuilder result = new StringBuilder("hi"); int index = original.indexOf('a'); /*1*/ result.setCharAt(0, original.charAt(0)); /*2*/ result.setCharAt(1, original.charAt(original.length() - 1)); /*3*/ result.insert(1, original.charAt(4)); /*4*/ result.append(original.substring(1, 4)); /*5*/ result.insert(3, (original.substring(index, index + 2) + " ")); System.out.println(result);//from w w w . j av a 2 s .com }