BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to divide a String in ‘n’ equal parts

Last Updated: July 8, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this article, we will write a java program to divide a string in ‘n’ equal parts. There are few things which we need to check before we divide the given string in equal parts. First thing we can check is to divide the number of characters in the string by the ‘n’, if the remainder is zero then this string can be divided in ‘n’ equal parts else it cannot be divided in equal parts.

Program to divide a given String in ‘n’ equal parts

Steps followed in the following program are:

  1. Find the number of characters in the string using length() method and divide it by n, if the remainder is not equal to 0 then print String cannot be divided else proceed to next step.
  2. Use the substring() method to divide the given string in ‘n’ equal parts and store the strings in a String array.
  3. Print the String array that contains the substrings of the given string.
public class JavaExample {
  public static void main(String[] args) {
    String str = "TextOneTextTwo";

    //Finds the length of the given string
    int len = str.length();

    //n represents the number of equal parts. Here 2
    //means the given string will be divided in two equal parts
    int n = 2;

    //numberOfChar represents the number of chars in divided strings
    int temp = 0, numberOfChar = len/n;
    String[] strParts = new String [n];
    //Checking whether the string can be divided in equal parts or not
    if(len % n != 0) {
      System.out.println("String cannot be divided into "+ n +" equal parts.");
    }
    else {
      for(int i = 0; i < len; i = i+numberOfChar) {
        //Dividing string using substring() method
        String subStr = str.substring(i, i+numberOfChar);
        strParts[temp] = subStr;
        temp++;
      }
      System.out.println(n + " equal parts of the given String are: ");
      for(int i = 0; i < strParts.length; i++) {
        System.out.println(strParts[i]);
      }
    }
  }
}

Output:

Java Program to divide a String in 'n' equal parts

Related Java Examples

  1. Java Program to check if two Strings are anagram or not
  2. Java Program to compare two Strings
  3. Java Program to perform bubble sort on Strings
  4. Java Program to count vowels and consonants in a String
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to find longest substring without repeating characters
  3. Java Program to find all subsets of a string
  4. Java program to print number of elements in an array
  5. Java Program to reverse the Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap