List of usage examples for org.apache.commons.lang3 StringUtils right
public static String right(final String str, final int len)
Gets the rightmost len characters of a String.
If len characters are not available, or the String is null , the String will be returned without an an exception.
From source file:de.gbv.ole.Marc21ToOleBulk.java
/** * Throws a MarcException if the parameter is not a valid isbn10 number * with valid check digit.//from www . j a v a2 s .c om * * Valid: "00", "19", "27", * "2121212124", * "9999999980", * "9999999999" * * @param isbn10 the number to validate */ static void validateIsbn10(final String isbn10) { if (StringUtils.isEmpty(isbn10)) { throw new MarcException("null or empty number"); } if (StringUtils.isWhitespace(isbn10)) { throw new MarcException("number expected, found only whitespace"); } if (StringUtils.containsWhitespace(isbn10)) { throw new MarcException( "number plus check digit expected, but contains " + "whitespace: '" + isbn10 + "'"); } if (isbn10.length() < 2) { throw new MarcException("number plus check digit expected, " + "but found: " + isbn10); } if (isbn10.length() > 10) { throw new MarcException("maximum length of number plus " + "check digit is 10 (9 + 1 check digit),\n" + "input is " + isbn10.length() + " characters long: " + isbn10); } String checkdigit = StringUtils.right(isbn10, 1); if (!StringUtils.containsOnly(checkdigit, "0123456789xX")) { throw new MarcException("check digit must be 0-9, x or X, " + "but is " + checkdigit); } String number = withoutCheckdigit(isbn10); if (!StringUtils.containsOnly(number, "0123456789")) { throw new MarcException("number before check digit must " + "contain 0-9 only, but is " + checkdigit); } String calculatedCheckdigit = isbn10checkdigit(Integer.parseInt(number)); if (!StringUtils.equalsIgnoreCase(checkdigit, calculatedCheckdigit)) { throw new MarcException("wrong checkdigit " + checkdigit + ", correct check digit is " + calculatedCheckdigit + ": " + isbn10); } }
From source file:com.feilong.core.lang.StringUtil.java
/** * [?]:??? <code>lastLenth</code> . * /* w w w . j a v a 2s . co m*/ * <h3>:</h3> * * <blockquote> * * <pre class="code"> * StringUtil.substringLast("jinxin.feilong", 5) = ilong * </pre> * * </blockquote> * * @param text * * @param lastLenth * ?? * @return <code>text</code> null, null<br> * {@code lastLenth<0}, {@link StringUtils#EMPTY}<br> * {@code text.length() <= lastLenth},text<br> * ?<code> text.substring(text.length() - lastLenth)</code> * @see org.apache.commons.lang3.StringUtils#right(String, int) */ public static String substringLast(final String text, int lastLenth) { return StringUtils.right(text, lastLenth); }
From source file:org.ambraproject.rhino.rest.controller.ArticleCrudController.java
/** * Calculate the date range using the specified rule. For example: * * <ul>// w ww . ja v a 2 s . com * <li>sinceRule=2y - 2 years</li> * <li>sinceRule=5m - 5 months</li> * <li>sinceRule=10d - 10 days</li> * <li>sinceRule=5h - 5 hours</li> * <li>sinceRule=33 - 33 minutes</li> * </ul> * * The method will result in a {@link java.util.Map Map} containing the following keys: * * <ul> * <li><b>fromDate</b> - the starting date * <li><b>toDate</b> - the ending date, which will be the current system date (i.e. now()) * </ul> * * @param sinceRule The rule to calculate the date range * * @return A {@link java.util.Map Map} */ public static final Map<String, LocalDateTime> calculateDateRange(String sinceRule) { if (StringUtils.isBlank(sinceRule)) { return ImmutableMap.of(); } final String timeDesignation = StringUtils.right(sinceRule, 1); long timeDelta = 0; try { // Assume last character is NOT a letter (i.e. all characters are digits). timeDelta = Long.parseLong(sinceRule); } catch (NumberFormatException exception) { // If an exception, then last character MUST have been a letter, // so we now exclude the last character and re-try conversion. try { timeDelta = Long.parseLong(sinceRule.substring(0, sinceRule.length() - 1)); } catch (NumberFormatException error) { log.warn("Failed to convert {} to a timeDelta/timeDesignation!", sinceRule); timeDelta = 0; } } if (timeDelta < 1) { return ImmutableMap.of(); } final LocalDateTime toDate = LocalDateTime.now(); final LocalDateTime fromDate; if (timeDesignation.equalsIgnoreCase("y")) { fromDate = toDate.minusYears(timeDelta); } else if (timeDesignation.equalsIgnoreCase("m")) { fromDate = toDate.minusMonths(timeDelta); } else if (timeDesignation.equalsIgnoreCase("d")) { fromDate = toDate.minusDays(timeDelta); } else if (timeDesignation.equalsIgnoreCase("h")) { fromDate = toDate.minus(timeDelta, ChronoUnit.HOURS); } else { fromDate = toDate.minus(timeDelta, ChronoUnit.MINUTES); } final ImmutableMap<String, LocalDateTime> dateRange = ImmutableMap.of(FROM_DATE, fromDate, TO_DATE, toDate); return dateRange; }
From source file:org.dbgl.util.searchengine.PouetSearchEngine.java
private static List<WebProfile> extractEntries(String html) { List<WebProfile> allEntries = new ArrayList<WebProfile>(); html = html.replaceAll("\\\\\"", "\""); int gameMatchEntryIndex = html.indexOf(HTML_MULTIPLE_RESULT_MARKER_START); if (gameMatchEntryIndex != -1) gameMatchEntryIndex += HTML_MULTIPLE_RESULT_MARKER_START.length(); while (gameMatchEntryIndex != -1) { String category = StringUtils .capitalize(extractNextContent(html, gameMatchEntryIndex, HTML_SPAN_OPEN, HTML_SPAN_CLOSE)); int startPlatformIdx = html.indexOf("<span class='platformiconlist'>", gameMatchEntryIndex); int gameTitleIdx = html.indexOf("<span class='prod'>", gameMatchEntryIndex); String gameTitleData = extractNextContent(html, gameTitleIdx, HTML_ANCHOR_OPEN, HTML_ANCHOR_CLOSE); String gameTitle = StringUtils.capitalize(unescapeHtml(removeAllTags(gameTitleData))); String url = extractNextHrefContentSingleQuotes(html, gameTitleIdx); url = absoluteUrl(HOST_NAME, url); gameMatchEntryIndex = html.indexOf(HTML_TD_OPEN, gameMatchEntryIndex); String developerName = extractNextContent(html, gameMatchEntryIndex + HTML_TD_OPEN.length(), HTML_ANCHOR_OPEN, HTML_ANCHOR_CLOSE); developerName = StringUtils.capitalize(developerName); String year = StringUtils .right(extractNextContent(html, gameMatchEntryIndex, "<td class='date'>", HTML_TD_CLOSE), 4); int rank = 0; try {// w ww . j a v a2s . co m String score = extractNextContent(html, gameMatchEntryIndex, "<td class='votes'>", " <img"); rank = (int) ((Double.parseDouble(score) + 1.0) * 50.0); } catch (Exception e) { } String details = html.substring(startPlatformIdx, gameTitleIdx); int platformIdx = details.indexOf(HTML_PLATFORM_MARKER); while (platformIdx != -1) { String platform = extractNextContent(details, platformIdx, HTML_SPAN_OPEN, HTML_SPAN_CLOSE); WebProfile gameEntry = new WebProfile(); gameEntry.setTitle(gameTitle); gameEntry.setUrl(url); gameEntry.setPlatform(platform); gameEntry.setPublisherName(""); gameEntry.setDeveloperName(developerName); gameEntry.setYear(year); gameEntry.setGenre(category); gameEntry.setRank(rank); gameEntry.setNotes(""); allEntries.add(gameEntry); platformIdx = details.indexOf(HTML_PLATFORM_MARKER, platformIdx + 1); } int endIdx = html.indexOf(HTML_GAME_END_MARKER, gameTitleIdx); gameMatchEntryIndex = html.indexOf(HTML_NEXT_RESULT_MARKER_START, endIdx + HTML_GAME_END_MARKER.length()); if (gameMatchEntryIndex != -1) gameMatchEntryIndex += HTML_NEXT_RESULT_MARKER_START.length(); } return allEntries; }
From source file:org.dbgl.util.searchengine.PouetSearchEngine.java
private List<WebProfile> extractSingleEntry(String html) { List<WebProfile> allEntries = new ArrayList<WebProfile>(); int canonicalIndex = html.indexOf("<link rel=\"canonical\""); String url = extractNextHrefContent(html, canonicalIndex); int titleIdx = html.indexOf("<span id='title'>", canonicalIndex); String gameTitleData = extractNextContent(html, titleIdx, "<big>", "</big>"); String title = StringUtils.capitalize(unescapeHtml(removeAllTags(gameTitleData))); String developerName = (html.indexOf("</big> by ", titleIdx) != -1) ? StringUtils.capitalize(extractNextContent(html, titleIdx, HTML_ANCHOR_OPEN, HTML_ANCHOR_CLOSE)) : ""; int platformIdx = html.indexOf("<td>platform :</td>", titleIdx); int categoryIdx = html.indexOf("<td>type :</td>", platformIdx); String category = StringUtils .capitalize(extractNextContent(html, categoryIdx, HTML_SPAN_OPEN, HTML_SPAN_CLOSE)); int yearIdx = html.indexOf("<td>release date :</td>", categoryIdx); String dateValue = extractNextContent(html, yearIdx + 24, HTML_TD_OPEN, HTML_TD_CLOSE); String year = !dateValue.contains("n/a") ? StringUtils.right(dateValue, 4) : ""; int scoreEndIdx = html.indexOf("<div id='alltimerank'>", yearIdx); int scoreStartIdx = html.lastIndexOf(" ", scoreEndIdx); int rank = 0; try {/*from ww w . ja v a 2s. c o m*/ String score = html.substring(scoreStartIdx + 6, scoreEndIdx); int l = score.indexOf("</li>"); if (l != -1) score = score.substring(0, l); rank = (int) ((Double.parseDouble(score) + 1.0) * 50.0); } catch (Exception e) { } String details = html.substring(platformIdx, categoryIdx); platformIdx = details.indexOf("<span class='platform "); while (platformIdx != -1) { String platform = extractNextContent(details, platformIdx, HTML_SPAN_OPEN, HTML_SPAN_CLOSE); WebProfile gameEntry = new WebProfile(); gameEntry.setTitle(title); gameEntry.setUrl(url); gameEntry.setPlatform(platform); gameEntry.setPublisherName(""); gameEntry.setDeveloperName(developerName); gameEntry.setYear(year); gameEntry.setGenre(category); gameEntry.setRank(rank); gameEntry.setNotes(""); allEntries.add(gameEntry); platformIdx = details.indexOf("<span class='platform ", platformIdx + 1); } return allEntries; }
From source file:org.efaps.esjp.admin.index.transformer.StringRight2Long.java
/** * Transform.//from w w w. ja v a 2 s. co m * * @param _object the object * @return the object * @throws EFapsException on error */ @Override public Object transform(final Object _object) throws EFapsException { Object ret = 0; if (_object instanceof String) { int i = 0; String subStr = "0"; while (NumberUtils.isDigits(subStr) && i - 1 < ((String) _object).length()) { i++; subStr = StringUtils.right((String) _object, i); } if (i - 1 > 0) { ret = Long.parseLong(StringUtils.right((String) _object, i - 1)); } } else { ret = _object; } return ret; }
From source file:org.icgc.dcc.release.job.fathmm.core.FathmmPredictor.java
private static Map<String, String> result(Map<String, Object> facade, Map<String, Object> probability, String aaChange, String weights) { Map<String, String> result = newHashMap(); float W = Float.parseFloat(facade.get(StringUtils.left(aaChange, 1)).toString()); float M = Float.parseFloat(facade.get(StringUtils.right(aaChange, 1)).toString()); float D = Float.parseFloat(probability.get("disease").toString()) + 1.0f; float O = Float.parseFloat(probability.get("other").toString()) + 1.0f; // The original calculation is in log2(...), this is just to change basis double score = Math.log(((1.0 - W) * O) / ((1.0 - M) * D)) / Math.log(2); // This is intended as well, the original script rounds before comparing against the threshold score = Math.floor(score * 100) / 100; result = newHashMap();/*from ww w . ja v a 2 s. co m*/ result.put("HMM", (String) facade.get("id")); result.put("Description", (String) facade.get("description")); result.put("Position", facade.get("position").toString()); result.put("W", String.valueOf(W)); result.put("M", String.valueOf(M)); result.put("D", String.valueOf(D)); result.put("O", String.valueOf(O)); result.put("Score", String.valueOf(score)); if (weights.equals("INHERITED")) { if (score <= -1.5f) { result.put("Prediction", "DAMAGING"); } else { result.put("Prediction", "TOLERATED"); } } return result; }
From source file:org.javabeanstack.util.Strings.java
/** * Reemplazara con un caracter "replace" cualquier valor dentro de "var" * que se encuentre comprendido entre caracteres "limit" * //from w ww. java 2 s . co m * @param var * @param limit lista de caracteres limitadores. * @param replace caracter con que se reemplazara las posiciones entre * los caracteres "limit" * @return cadena procesada. */ public static String varReplace(String var, String limit, String replace) { String result = var; // Si esta vacio la variable if (Strings.isNullorEmpty(var) || Strings.isNullorEmpty(limit)) { return var; } String limit1, limit2; if (limit.length() == 2) { limit1 = StringUtils.left(limit, 1); limit2 = StringUtils.right(limit, 1); } else { limit1 = limit.substring(0, 1); limit2 = limit.substring(0, 1); } int c = 0, p1 = 0, p2 = 0, c2 = 0; while (true) { c = c + 1; p1 = findString(limit1, result, c); if (limit1.equals(limit2)) { c = c + 1; } p2 = findString(limit2, result, c); // Verificar que el caracter no sea un limitador interno if (!limit1.equals(limit2)) { c2 = c; while (p1 >= 0) { int innerLimit1 = occurs(limit1, result.substring(p1, p2)); int innerLimit2 = occurs(limit2, result.substring(p1, p2 + 1)); if (innerLimit2 == 0) { break; } if (innerLimit1 - innerLimit2 != 0) { c2 = c2 + innerLimit1 - innerLimit2; p2 = findString(limit2, result, c2); } else { break; } } } if (p1 == -1 || p2 == -1) { break; } result = StringUtils.left(result, p1 + 1) + StringUtils.repeat(replace, p2 - p1 - 1) + result.substring(p2); } return result; }
From source file:org.javabeanstack.util.Strings.java
/** * Toma los n caracteres del lado derecho de un string * @param str variable string/*w w w . jav a 2s .c o m*/ * @param len cantidad de caracteres * @return cantidad de caracteres del lado derecho de una variable alfanumrica. */ public static String right(String str, int len) { return StringUtils.right(str, len); }
From source file:org.noroomattheinn.visibletesla.dialogs.SelectVehicleDialog.java
public static Vehicle select(Stage stage, List<Vehicle> vehicleList) { int selectedVehicleIndex = 0; if (vehicleList.size() != 1) { // Ask the user to select a vehicle List<String> cars = new ArrayList<>(); for (Vehicle v : vehicleList) { StringBuilder descriptor = new StringBuilder(); descriptor.append(StringUtils.right(v.getVIN(), 6)); descriptor.append(": "); descriptor.append(v.getOptions().paintColor()); descriptor.append(" "); descriptor.append(v.getOptions().batteryType()); cars.add(descriptor.toString()); }/*from w ww . ja va 2s . c o m*/ String selection = Dialogs.showInputDialog(stage, "Vehicle: ", "You lucky devil, you've got more than 1 Tesla!", "Select a vehicle", cars.get(0), cars); selectedVehicleIndex = cars.indexOf(selection); if (selectedVehicleIndex == -1) { selectedVehicleIndex = 0; } } return vehicleList.get(selectedVehicleIndex); }