List of usage examples for org.apache.commons.lang3 StringUtils countMatches
public static int countMatches(final CharSequence str, final char ch)
Counts how many times the char appears in the given string.
A null or empty ("") String input returns 0 .
StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", 0) = 0 StringUtils.countMatches("abba", 'a') = 2 StringUtils.countMatches("abba", 'b') = 2 StringUtils.countMatches("abba", 'x') = 0
From source file:com.chiorichan.factory.interpreters.GSPInterpreter.java
@Override public Object eval(EvalMetaData meta, String fullFile, ShellFactory shellFactory, ByteArrayOutputStream bs) throws Exception { int fullFileIndex = 0; String[] dontStartWith = new String[] { "println", "print", "echo", "def", "import", "if", "for", "do", "}", "else", "//", "/*", "\n", "\r" }; StringBuilder output = new StringBuilder(); while (fullFileIndex < fullFile.length()) { int startIndex = fullFile.indexOf(MARKER_START, fullFileIndex); if (-1 != startIndex) { // Append all the simple text until the marker String fragment = escapeFragment(fullFile.substring(fullFileIndex, startIndex)); if (!fragment.isEmpty()) output.append(fragment); int endIndex = fullFile.indexOf(MARKER_END, Math.max(startIndex, fullFileIndex)); if (endIndex == -1) throw new EvalFactoryException(new IOException("Marker `<%` was not closed after line " + (StringUtils.countMatches(output.toString(), "\n") + 1) + ", please check your source file and try again."), shellFactory); fragment = fullFile.substring(startIndex + MARKER_START.length(), endIndex); boolean appendPrint = true; for (String s : dontStartWith) if (fragment.trim().startsWith(s) || fragment.startsWith(s) || fragment.trim().isEmpty()) appendPrint = false; if (appendPrint) fragment = "print " + fragment; if (!fragment.isEmpty()) output.append(fragment + "; "); // Position index after end marker fullFileIndex = endIndex + MARKER_END.length(); } else {/*from w w w . j av a2 s . c o m*/ String fragment = escapeFragment(fullFile.substring(fullFileIndex)); if (!fragment.isEmpty()) output.append(fragment); // Position index after the end of the file fullFileIndex = fullFile.length() + 1; } } meta.source = output.toString(); return interpret(shellFactory, output.toString(), meta); }
From source file:brut.androlib.decode.DoubleExtensionUnknownFileTest.java
@Test public void multipleExtensionUnknownFileTest() throws BrutException, IOException { String apk = "issue1244.apk"; // decode issue1244.apk ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk)); ExtFile decodedApk = new ExtFile(sTmpDir + File.separator + apk + ".out"); apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out")); apkDecoder.decode();/*from www. j a va 2 s. co m*/ MetaInfo metaInfo = new Androlib().readMetaFile(decodedApk); for (String string : metaInfo.doNotCompress) { if (StringUtils.countMatches(string, ".") > 1) { assertTrue(string.equalsIgnoreCase("assets/bin/Data/sharedassets1.assets.split0")); } } }
From source file:com.seleniumtests.ut.core.TestLogActions.java
/** * Check that when calling a method with password in it, this is masked * parameter name contains 'password' but not only that to check replacement is done on similar strings *//*ww w. j a va 2 s .com*/ @Test(groups = { "ut" }) public void testPassworkMasking() { testPage._setPassword("someText"); TestStep step = TestLogging.getTestsSteps().get(TestLogging.getCurrentTestResult()).get(2); // all occurences of the password have been replaced Assert.assertFalse(step.toString().contains("someText")); Assert.assertEquals(StringUtils.countMatches(step.toString(), "******"), 2); }
From source file:io.fd.maintainer.plugin.util.MaintainersIndex.java
private static int getPathLength(final String path) { return StringUtils.countMatches(path, "/"); }
From source file:com.feilong.commons.core.util.StringUtilTest.java
/** * Search count.// ww w. ja v a2s.co m */ @Test public void testSearchCount() { String source = "jiiiiiinxin.feilong"; log.info(StringUtil.searchTimes(source, "i") + ""); log.info(StringUtils.countMatches(source, "i") + ""); log.info(StringUtil.searchTimes(source, "in") + ""); log.info(StringUtil.searchTimes(source, "ii") + ""); log.info(StringUtil.searchTimes(source, "xin") + ""); Assert.assertEquals(1, StringUtil.searchTimes("xin", "xin")); Assert.assertEquals(1, StringUtil.searchTimes("xin", "i")); Assert.assertEquals(2, StringUtil.searchTimes("xiin", "i")); Assert.assertEquals(2, StringUtil.searchTimes("xiiiin", "ii")); }
From source file:br.com.fidias.chance4j.text.ParagraphTest.java
@Test public void randomParagraph() throws ChanceException { for (int i = 0; i < 1000; i++) { paragraph = chance.paragraph();/*from w w w . j av a2s .c o m*/ String replaceChars = StringUtils.replaceChars(paragraph, PUNCTUATION, replaceSymbols); int countMatches = StringUtils.countMatches(replaceChars, '_'); assertTrue("random paragraph 5 sentences", countMatches >= MIN_SENTENCES_FOR_PARAGRAPH && countMatches <= MAX_SENTENCES_FOR_PARAGRAPH); } }
From source file:com.xpn.xwiki.web.XWikiRequestProcessor.java
/** * {@inheritDoc}//from w w w . java 2 s .com * * @see org.apache.struts.action.RequestProcessor#processPath(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override protected String processPath(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { String result = super.processPath(httpServletRequest, httpServletResponse); if ("1".equals(XWikiConfigurationService.getProperty("xwiki.virtual.usepath", "0", getServletContext()))) { // Remove /wikiname part if the struts action is /wiki if (httpServletRequest.getServletPath().equals("/" + XWikiConfigurationService .getProperty("xwiki.virtual.usepath.servletpath", "wiki", getServletContext()))) { int wikiNameIndex = result.indexOf("/", 1); if (wikiNameIndex == -1) { result = ""; } else { result = result.substring(wikiNameIndex); } } } if (StringUtils.countMatches(result, "/") <= 2) { if (result.startsWith("/xmlrpc/")) { return "/xmlrpc/"; } else { return "/view/"; } } else { return result.substring(0, result.indexOf("/", 1) + 1); } }
From source file:MiGA.motifStats.java
public void update(int re) { count++;// w w w.j a v a 2 s . co m repeats += re; mbp_len += re * motif.length(); avg_len = (double) mbp_len / count; if (re * motif.length() > max_len) { max_len = re * motif.length(); } avg_repeats = (double) repeats / count; if (motif.contains("A")) { perA = (double) (mbp_len / motif.length()) * StringUtils.countMatches(motif, "A") / mbp_len * 100; } if (motif.contains("T")) { perT = (double) (mbp_len / motif.length()) * StringUtils.countMatches(motif, "T") / mbp_len * 100; } if (motif.contains("G")) { perG = (double) (mbp_len / motif.length()) * StringUtils.countMatches(motif, "G") / mbp_len * 100; } if (motif.contains("C")) { perC = (double) (mbp_len / motif.length()) * StringUtils.countMatches(motif, "C") / mbp_len * 100; } lens.add(re * motif.length()); }
From source file:com.acceleratedio.pac_n_zoom.DrawSVG.java
public ArrayList<ImageView> DrawSVG(Context context, ImageView orgnlImageView, RelativeLayout rel_anm_lo) { paint.setColor(Color.WHITE);//from w w w . j ava 2s .co m paint.setStyle(Paint.Style.FILL); LoadSVG.SVGData data = AnimActivity.svg_data; ArrayList<ImageView> imgViews = new ArrayList<ImageView>(); imgViews.add(orgnlImageView); if (data.symbl != null) { ArrayList<String> sprt_ordr = data.svg.ordr; int sym_mbr = 0; Bitmap bitmap; Canvas canvas; // - Loop through the sprites int sprt_nmbr = sprt_ordr.size(); data.svg.initScl = new float[sprt_nmbr]; float[] initScl = data.svg.initScl; for (int sprt_mbr = 1; sprt_mbr < sprt_nmbr; sprt_mbr += 1) { String sprt_id = sprt_ordr.get(sprt_mbr); // e.g., id="g2_0" if (Integer.parseInt(sprt_id.substring(sprt_id.indexOf('_') + 1)) > 0) { // The symbol is already drawn; replicate the view String init_sprt = sprt_id.substring(0, sprt_id.indexOf('_') + 1) + '0'; String svg_ordr = data.svg.svg_ordr; String cnt_str = svg_ordr.substring(0, svg_ordr.indexOf(init_sprt)); ImageView init_vw = imgViews.get(StringUtils.countMatches(cnt_str, ",")); Bitmap initBmp = ((BitmapDrawable) init_vw.getDrawable()).getBitmap(); bitmap = Bitmap.createBitmap(initBmp.getWidth(), initBmp.getHeight(), initBmp.getConfig()); canvas = new Canvas(bitmap); canvas.save(Canvas.MATRIX_SAVE_FLAG); xfrmInit crt_sprt = getInitSpriteAttrib(sprt_id); canvas.scale(crt_sprt.scl, crt_sprt.scl); initScl[sprt_mbr] = crt_sprt.scl; canvas.translate(0, 0); } else { // The symbol needs to be drawn; a new view is used bitmap = getCreatBmp(rel_anm_lo); canvas = new Canvas(bitmap); canvas.save(Canvas.MATRIX_SAVE_FLAG); // - Set the init values xfrmInit crt_sprt = getInitSpriteAttrib(sprt_id); canvas.scale(crt_sprt.scl, crt_sprt.scl); initScl[sprt_mbr] = crt_sprt.scl; canvas.translate(0, 0); // - Draw the bitmap LoadSVG.symbol crt_sym = data.symbl.get(sym_mbr); ArrayList<LoadSVG.path> pths = crt_sym.pths; int pth_nmbr = pths.size(); // Loop through the paths for (int pth_mbr = 0; pth_mbr < pth_nmbr; pth_mbr += 1) { LoadSVG.path crt_pth = pths.get(pth_mbr); final Path path = new Path(); final Paint paint = new Paint(); /* Debug if (pth_mbr + 1 == pth_nmbr) { String log_str = "Paths: pth_mbr = " + String.valueOf(pth_mbr) + "; color = " + crt_pth.clr; Log.d("DrawSVG", log_str); } */ paint.setColor(Color.parseColor(crt_pth.clr)); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL_AND_STROKE); ld_pth_pnts(crt_pth.pth, path); path.close(); path.setFillType(Path.FillType.EVEN_ODD); canvas.drawPath(path, paint); } canvas.restore(); sym_mbr += 1; } ImageView iv = new ImageView(context); iv.setImageBitmap(bitmap); RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); iv.draw(canvas); iv.setLayoutParams(rlp); iv.setBackgroundColor(Color.TRANSPARENT); imgViews.add(iv); } // sprites } // if symbol return imgViews; }
From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java
/** * @param string//from w ww.j a v a2s . c om * @return * */ private static String replaceWhitespaces(String string) { String word = " OR "; String result = ""; List<String> allMatches = new ArrayList<>(); if (StringUtils.countMatches(string, '"') > 0 && ((StringUtils.countMatches(string, '"') & 1) == 1)) { string = string.concat("\""); } Matcher m = Pattern.compile("[^,]+").matcher(string); while (m.find()) { allMatches.add(m.group()); } for (String sub : allMatches) { sub = sub.trim(); result = result.concat("\"" + sub + "\"" + word); } result = result.substring(0, result.length() - 4); // remove last "OR" return result; }