Here is a very simple Java Program equivalent to Python’s string_splosion
function.
package com.crunchify.tutorials; /** * @author Crunchify.com * */ public class CrunchifyStringSplosion { public static void main(String[] args) { System.out.println("Result: " + occurance("Code")); } public static String occurance(String str) { String temp = ""; for (int i = 0; i < str.length() + 1; i++) temp += str.substring(0, i); return temp; } }
Result:
Result: CCoCodCode
Method-2
Example with StringBuilder.
package crunchify.com.tutorials; public class CrunchifyStringTransformation { public static String crunchifyStringTransform(String input) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.length(); i++) { sb.append(input.substring(0, i + 1)); } return sb.toString(); } public static void main(String[] args) { String input = "Code"; String output = crunchifyStringTransform(input); System.out.println("Input: " + input); System.out.println("Output: " + output); } }
Eclipse Console Result:
Input: Code Output: CCoCodCode Process finished with exit code 0