0

I have one input String to the program and I need to execute this String into my method, but I want to execute it if the character String is less than or equal to 20 characters, so I want to split this String into multiple Strings if the string is longer than 20 characters.
That is, the number of characters input String is 90 characters then become a 5 String 20 + 20 + 20 + 20 + 10 = 90.

I need each 20 characters String and last String do this code:

try {
            enMessage = AES.encrypt(key, message);
            sendSMS(contact, enMessage);
        } catch (Exception e) 

so its could make each 20 characters is one message.

Viema
  • 21
  • 2
  • 1
    Usage of what you're searchin for is shown here: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – fabianski Jul 24 '15 at 07:55

4 Answers4

1

You can use String's substring method to split the strings you want to.

You can read about how to use it here

Java String Documentation

Vincent Paing
  • 2,308
  • 3
  • 17
  • 29
1

The best example of such code I have seen so far on this site is:

public class StringSplitter {

    /* regex was stolen from other stackoverflow answer*/
    public static void main(String[] args) {
        for (String str : "123a4567a8sdfsdfsdgasfsdfsdgsdcvsdfdgdfsdf9".split("(?<=\\G.{20})"))
            System.out.format("\'%s\'%n", str);
    }

}
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
Lysenko Andrii
  • 496
  • 4
  • 11
  • how to make each 20 characters become variable.. because here every 20 characters will be given method. thanks – Viema Jul 24 '15 at 10:09
  • @Viema, in the code above you can see a for-each loop. Each new String object consisting of 20 or less characters is stored in "str" variable – Lysenko Andrii Jul 24 '15 at 10:33
0

Try this:

ArrayList<String> allstrings = new ArrayList<>();

if(inputstring.length() < 20) {
    //no spliting
} else {
    //if 90 char than 90/20=4.5
    float numberOfspliting = inputstring.length / 20f; 

    for(int i = 0; i < numberofspliting; i++){
        String split = inputstring.substring(i * 20, i * 20 + 20);
        allstrings.add(split);
    }
    //like 4.5-4=0.5
    float leftcharacters = numberofspliting - (int)numberofspliting;

    String lastsplit = inputstring.substring((int)numberofspliting * 20, (int)numberofspliting * 20 + leftcharacters * 20f);

    allstrings.add(lastsplit);

}//end if
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
has19
  • 1,307
  • 16
  • 31
  • still getting error in lastsplit and I don't know how to do method when all string stored in ArrayList. thanks – Viema Jul 24 '15 at 10:10
0

You can try doing this:

package com.me;

import java.util.*;
import java.lang.*;
import java.io.*;

public class Test
{
        public static List<String> splitMyString(String textValue, int textSize) {        
        ArrayList<String> myNewList= new ArrayList<String>((textValue.length() + textSize- 1) / textSize);

        for (int start = 0; start < textValue.length(); start += textSize) {
            myNewList.add(textValue.substring(start, Math.min(textValue.length(), start + textSize)));
        }
        System.out.println(myNewList.toString());
        return myNewList;
     }
    public static void main(String[] args) {

        Test.splitMyString("1234546512312312312312365", 5);

    }
}

Output:

Success time: 0.1 memory: [12345, 46512, 31231, 23123, 12365]

sTg
  • 4,313
  • 16
  • 68
  • 115