List of usage examples for java.lang Character isWhitespace
public static boolean isWhitespace(int codePoint)
From source file:Main.java
public static String fillTextBox(TextPaint paint, int fragmentWidth, String source, int start) { StringBuilder sb = new StringBuilder(); final int length = source.length(); int indexLeft = start; int indexRight = start + 1; int lastWhiteSpaceL = 0; int lastWhiteSpaceR = 0; while (paint.measureText(sb.toString()) < fragmentWidth && (indexLeft >= 0 || indexRight < length)) { if (indexLeft >= 0) { char c = source.charAt(indexLeft); if (Character.isWhitespace(c)) lastWhiteSpaceL = indexLeft; sb.insert(0, c);/*w ww . ja va 2s . c o m*/ indexLeft--; } if (indexRight < length) { char c = source.charAt(indexRight); if (Character.isWhitespace(c)) lastWhiteSpaceR = indexRight; sb.append(c); indexRight++; } } if (indexLeft >= 0) { // Delete first word part sb.delete(0, lastWhiteSpaceL - indexLeft); sb.insert(0, "..."); indexLeft = lastWhiteSpaceL - 3; // Set new index left } if (indexRight < length) { // Delete last word part sb.delete(lastWhiteSpaceR - (indexLeft + 1), sb.length()); sb.append("..."); } return sb.toString(); }
From source file:net.jcreate.e3.table.decorator.URLDecorator.java
public Object decorate(Object pValue, Cell pCell) throws Exception { if (pValue == null) { return null; }/*from ww w . j av a2s .c om*/ String work = pValue.toString(); int urlBegin; StringBuffer buffer = new StringBuffer(); // First check for email addresses. while ((urlBegin = work.indexOf('@')) != -1) { int start = 0; int end = work.length() - 1; // scan backwards... for (int j = urlBegin; j >= 0; j--) { if (Character.isWhitespace(work.charAt(j))) { start = j + 1; break; } } // scan forwards... for (int j = urlBegin; j <= end; j++) { if (Character.isWhitespace(work.charAt(j))) { end = j - 1; break; } } String email = work.substring(start, end + 1); buffer.append(work.substring(0, start)).append("<a href=\"mailto:") //$NON-NLS-1$ .append(email + "\">") //$NON-NLS-1$ .append(email).append("</a>"); //$NON-NLS-1$ if (end == work.length()) { work = TagConstants.EMPTY_STRING; } else { work = work.substring(end + 1); } } work = buffer.toString() + work; buffer = new StringBuffer(); // Now check for urls... while ((urlBegin = work.indexOf(URL_DELIM)) != -1) { // scan backwards... int fullUrlBegin = urlBegin; StringBuffer prefixBuffer = new StringBuffer(10); for (int j = fullUrlBegin - 1; j >= 0; j--) { if (Character.isWhitespace(work.charAt(j))) { fullUrlBegin = j + 1; break; } fullUrlBegin = j; prefixBuffer.append(work.charAt(j)); } if (!ArrayUtils.contains(URLS_PREFIXES, StringUtils.reverse(prefixBuffer.toString()))) { buffer.append(work.substring(0, urlBegin + 3)); work = work.substring(urlBegin + 3); continue; } int urlEnd = work.length(); // scan forwards... for (int j = urlBegin; j < urlEnd; j++) { if (Character.isWhitespace(work.charAt(j))) { urlEnd = j; break; } } String url = work.substring(fullUrlBegin, urlEnd); buffer.append(work.substring(0, fullUrlBegin)).append("<a href=\"")//$NON-NLS-1$ .append(url).append("\">")//$NON-NLS-1$ .append(url).append("</a>"); //$NON-NLS-1$ if (urlEnd >= work.length()) { work = TagConstants.EMPTY_STRING; } else { work = work.substring(urlEnd); } } buffer.append(work); return buffer.toString(); }
From source file:com.puppycrawl.tools.checkstyle.api.Utils.java
/** * Returns whether the specified string contains only whitespace up to the * specified index./*from w w w .j a va2 s . c o m*/ * * @param index index to check up to * @param line the line to check * @return whether there is only whitespace */ public static boolean whitespaceBefore(int index, String line) { for (int i = 0; i < index; i++) { if (!Character.isWhitespace(line.charAt(i))) { return false; } } return true; }
From source file:com.github.jknack.amd4j.WhiteMinifier.java
/** * Remove spaces and lines./*ww w. j av a 2 s .c o m*/ * * @param source JavaScript code without comments. * @return The new source code. */ private CharSequence strip(final String source) { StringBuilder buffer = new StringBuilder(); int i = 0; while (i < source.length()) { char ch = source.charAt(i); if (ch == '\'' || ch == '"') { String literal = stringLiteral(source, ch, i); buffer.append(literal); i += literal.length() - 1; } else if (ch == '/') { String regex = regex(source, i); buffer.append(regex); i += regex.length() - 1; } else if (Character.isWhitespace(ch)) { if (i + 1 < source.length() && Character.isJavaIdentifierStart(source.charAt(i + 1))) { // keep white between keywords or identifiers. buffer.append(' '); } } else { buffer.append(ch); } i++; } return buffer; }
From source file:com.pfarrell.utils.misc.TextTools.java
/** * make a string with no whitespace at all * @param arg string/*from w w w .j a va 2s . co m*/ * @return string with no whitespace */ public static String noWhiteSpace(String arg) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arg.length(); i++) { int c = arg.codePointAt(i); if (!Character.isWhitespace(c)) { sb.appendCodePoint(c); } } return sb.toString(); }
From source file:TextUtilities.java
/** * Trims the specified string on the right only. *///from w w w . j a va 2s .c o m public static String trimRight(String s) { StringBuffer buf = new StringBuffer(s); int length = buf.length(); while (Character.isWhitespace(buf.charAt(length - 1))) buf.setLength(--length); return buf.toString(); }
From source file:com.nesscomputing.syslog4j.impl.message.modifier.escape.HTMLEntityEscapeSyslogMessageModifier.java
/** * escapeHtml(String) is based partly on the article posted here: http://www.owasp.org/index.php/How_to_perform_HTML_entity_encoding_in_Java * with the addition of common characters and modifications for Java 1.4 support. * * @param message//from w ww .jav a 2 s . c o m * @return Returns a message where any HTML entity characters are escaped. */ public static String escapeHtml(String message) { StringBuffer b = new StringBuffer(message.length()); for (int i = 0; i < message.length(); i++) { char ch = message.charAt(i); if (ch == '<') { b.append("<"); } else if (ch == '>') { b.append(">"); } else if (ch == '"') { b.append("""); } else if (ch == '\'') { b.append("'"); } else if (ch == '&') { b.append("&"); } else if (ch >= ' ' && ch <= '~') { b.append(ch); } else if (Character.isWhitespace(ch)) { b.append("&#").append((int) ch).append(";"); } else if (Character.isISOControl(ch)) { continue; // Ignore character } else if (Character.isDefined(ch)) { b.append("&#").append((int) ch).append(";"); } } return b.toString(); }
From source file:edu.illinois.cs.cogcomp.annotation.TextAnnotationFromTokenizationTest.java
private Tokenizer.Tokenization getTokenization(String rawText) { String[] sentences = this.rawText.split("\\n"); String[] tokens = new String[0]; List<IntPair> characterOffsets = new ArrayList<>(); int[] sentenceEndArray = new int[sentences.length]; int sentenceCharOffset = 0; int lastTokenCount = 0; for (int iSentence = 0; iSentence < sentences.length; iSentence++) { String sentence = sentences[iSentence]; String[] sentenceTokens = sentence.split("\\s"); tokens = (String[]) ArrayUtils.addAll(tokens, sentenceTokens); int charOffsetBegin = sentenceCharOffset; int charOffsetEnd = sentenceCharOffset; for (int i = 0; i < sentence.length(); i++) { char c = sentence.charAt(i); if (Character.isWhitespace(c)) { charOffsetEnd = sentenceCharOffset + i; IntPair tokenOffsets = new IntPair(charOffsetBegin, charOffsetEnd); characterOffsets.add(tokenOffsets); charOffsetBegin = charOffsetEnd + 1; }//from w ww .j a va 2 s. c o m } charOffsetEnd = sentenceCharOffset + sentence.length(); IntPair tokenOffsets = new IntPair(charOffsetBegin, charOffsetEnd); characterOffsets.add(tokenOffsets); sentenceCharOffset = charOffsetEnd + 1; lastTokenCount += sentenceTokens.length; sentenceEndArray[iSentence] = lastTokenCount; } IntPair[] charOffsetArray = new IntPair[characterOffsets.size()]; for (int i = 0; i < characterOffsets.size(); i++) { charOffsetArray[i] = characterOffsets.get(i); } Tokenizer.Tokenization tokenization = new Tokenizer.Tokenization(tokens, charOffsetArray, sentenceEndArray); return tokenization; }
From source file:Main.java
/** * Put common code to deepTrim(String) and deepTrimToLower here. * //from w w w. j a v a 2 s .c om * @param str * the string to deep trim * @param toLowerCase * how to normalize for case: upper or lower * @return the deep trimmed string * @see StringTools#deepTrim( String ) * * TODO Replace the toCharArray() by substring manipulations */ public static final String deepTrim(String str, boolean toLowerCase) { if ((null == str) || (str.length() == 0)) { return ""; } char ch; char[] buf = str.toCharArray(); char[] newbuf = new char[buf.length]; boolean wsSeen = false; boolean isStart = true; int pos = 0; for (int i = 0; i < str.length(); i++) { ch = buf[i]; // filter out all uppercase characters if (toLowerCase) { if (Character.isUpperCase(ch)) { ch = Character.toLowerCase(ch); } } // Check to see if we should add space if (Character.isWhitespace(ch)) { // If the buffer has had characters added already check last // added character. Only append a spc if last character was // not whitespace. if (wsSeen) { continue; } else { wsSeen = true; if (isStart) { isStart = false; } else { newbuf[pos++] = ch; } } } else { // Add all non-whitespace wsSeen = false; isStart = false; newbuf[pos++] = ch; } } return (pos == 0 ? "" : new String(newbuf, 0, (wsSeen ? pos - 1 : pos))); }
From source file:Main.java
/** * Escapes the node portion of a JID according to "JID Escaping" (JEP-0106). * Escaping replaces characters prohibited by node-prep with escape sequences, * as follows:<p>// www . j a v a 2 s. c o m * * <table border="1"> * <tr><td><b>Unescaped Character</b></td><td><b>Encoded Sequence</b></td></tr> * <tr><td><space></td><td>\20</td></tr> * <tr><td>"</td><td>\22</td></tr> * <tr><td>&</td><td>\26</td></tr> * <tr><td>'</td><td>\27</td></tr> * <tr><td>/</td><td>\2f</td></tr> * <tr><td>:</td><td>\3a</td></tr> * <tr><td><</td><td>\3c</td></tr> * <tr><td>></td><td>\3e</td></tr> * <tr><td>@</td><td>\40</td></tr> * <tr><td>\</td><td>\5c</td></tr> * </table><p> * * This process is useful when the node comes from an external source that doesn't * conform to nodeprep. For example, a username in LDAP may be "Joe Smith". Because * the <space> character isn't a valid part of a node, the username should * be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" * after case-folding, etc. has been applied).<p> * * All node escaping and un-escaping must be performed manually at the appropriate * time; the JID class will not escape or un-escape automatically. * * @param node the node. * @return the escaped version of the node. */ public static String escapeNode(String node) { if (node == null) { return null; } StringBuilder buf = new StringBuilder(node.length() + 8); for (int i = 0, n = node.length(); i < n; i++) { char c = node.charAt(i); switch (c) { case '"': buf.append("\\22"); break; case '&': buf.append("\\26"); break; case '\'': buf.append("\\27"); break; case '/': buf.append("\\2f"); break; case ':': buf.append("\\3a"); break; case '<': buf.append("\\3c"); break; case '>': buf.append("\\3e"); break; case '@': buf.append("\\40"); break; case '\\': buf.append("\\5c"); break; default: { if (Character.isWhitespace(c)) { buf.append("\\20"); } else { buf.append(c); } } } } return buf.toString(); }