Tuesday, 29 March 2016

Split Text for specified number of characters


/**
  * This method is used to break the text for specified no of characters
  * @param message , Which we have to break
  * @param noOfCharacters , No of characters on which we have to break the message
  * @return splited Text
  */
  public static String splitText(String text, int noOfChar , char splitChar) {
        if(text != null) {
          StringBuffer stBuffer = new StringBuffer(1024);
          int length = text.length(); // Get the length of the text
          // count number of lines in output text
          int lines = length / noOfChar;  
          int startIndex = 0;
          int endIndex = startIndex + noOfChar;
          for (int counter = 0; counter < lines; counter++) {
             stBuffer.append(text.substring(startIndex, endIndex));  // add token
             stBuffer.append(splitChar);  // add split char
             // increase the start and end index
             startIndex = endIndex;  
             endIndex = endIndex + noOfChar;
          }
            
          // add the remaining part of text
          stBuffer.append(text.substring(startIndex));  
          return stBuffer.toString();
        } else {
            return "";
        }
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.