スキップしてメイン コンテンツに移動

投稿

ラベル(string)が付いた投稿を表示しています

Java: Count String Occurrence

Here is the code for count up string given as a second parameter appears on string given as a first parameter. public final static int countOccurrence(String src, String search) { for(int count = 0, fromIndex = 0;;) { fromIndex = src.indexOf(search, fromIndex); if(fromIndex < 0) return count; fromIndex++; count++; } }

Java: org.w3c.dom.Node to Formatted Xml String

package com.dukesoftware.xml; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; public class XmlUtils { public static String toString(Node node) throws TransformerException { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } }

Java: Joining String Array with Separator (Equilvarent to PHP implode)

If you would like to join array with a separator, what will you do? e.g. Joining "a","b","c" with separator "," to "a,b,c". In PHP, you just use implode function. Actually PHP has a lot of functions like manipulating, converting string and array. In java, these kind of functions are not provided in official JDK. If you are familiar with Java Libraries, you just use them. - Joiner in google Guava, StringUtils in Apache Coommons. I will show you quick code snippet for jonining or imploding java string array with a separator. private static String join(String[] pieces, String separator) { if(pieces.length == 0) return ""; StringBuilder sb = new StringBuilder(pieces[0]); for(int i = 1; i < pieces.length; i++) { sb.append(separator).append(pieces[i]); } return sb.toString(); } If the number of array elements is zero, the method returns empty string, but if you would not prefer this

Regex: Detect Repeated String Chunks

Target strings I am trying to detect string which contains repeated string pattern like below. .....ABCDABCDABCD..... "ABCD" is just an example, it can be any fixed length of string chunk. Solution Regex Thinking for a bit and finally reached the solution regex which meets my demand is something like below. (.{4,})\\1{3,} The above regex matches string whose length of the chunk is more than 4 and it should be repeated equal or more than 4 times. In more general regex is below (using pattern formatting). String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1) Java Code Ok you know I am Java lover, I will show you full regex code with test cases. If you found any bugs on the code please feel free to comment. public static final boolean isRepeatedStrIn(String input, int minChunkLen, int times) { return input.matches(String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1)); } package com.dukesoftware.utils.common; import java.util.Array

ActionScript 3.0: String Utility Methods (trim, startsWith, endsWith etc.)

public class StringUtils { public function StringUtils() { } public static function concatAsString(array:Array):String { var ret:String = ""; for each(var str:String in array) { ret += str; } return ret; } public static function concatAllAsString(delimita:String, ...strs):String { if (strs == null || strs.length == 0) { return ""; } var ret:String = ""; for each(var str:String in strs) { ret += delimita + str } return ret.substr(1, ret.length - 1); } public static function trim(src:String):String { return src.replace(/^[\s\t]+(.+)[\s\t]+$/, "$1"); } public static function endsWith(str:String, seacrh:String):Boolean { return str.length > seacrh.length && str.substr(seacrh.length-1, seacrh.length) === seacrh; } public static function startsWith(prefix:String, testStr:String):Boolean { return testStr.length >= prefix.length && testStr.substr(0, prefix.len

JavaのString#hasCodeの計算方法

Java2のバージョンが1.2以前の場合、Stringオブジェクトの16文字以上の部分はhashCodeの計算に用いられません。そのためhashCodeの衝突が起きやすくなっています。 Java2のバージョンが1.3以降の場合、hashCodeの計算方法が改良され、16文字以上の部分も計算に利用されるようになりました。 BOOLEANLABEL さんの String.hashCode() の変遷 が参考になります。 バージョン1.2以前での衝突するhashCodeのBugレポートが Bug ID: 1258091 String.hashCode() produces the same value for too many unequal strings. にあります。

Ruby, Python, JavaScript, Perl, C++ 比較

Ruby, Python, JavaScript, Perl, C++間の比較 bkブログ さんによくまとまっています。 配列操作の比較表 文字列操作の比較表 JavaとC#のGenericsの比較 C#とJavaのGenericsに関する比較は まじかんと さんの ジェネリック: Java vs C# によくまとまっています。ちなみにJava 5.0のGenericsに関する説明のpdfが ここ からダウンロードできます。