根据长度拆分字符串
Split a String depending on its length
我有一个输入字符串到程序,我需要将这个字符串执行到我的方法中,但是如果字符串小于或等于20个字符我想执行它,所以我想拆分这个字符串如果字符串超过 20 个字符,则分成多个字符串。
即输入String的字符数为90个字符则变成5个String 20 + 20 + 20 + 20 + 10 = 90.
我需要每个 20 个字符的字符串和最后一个字符串执行此代码:
try {
enMessage = AES.encrypt(key, message);
sendSMS(contact, enMessage);
} catch (Exception e)
所以它可以让每 20 个字符是一条消息。
您可以使用String的substring方法来拆分您想要的字符串。
您可以在此处了解如何使用它
试试这个:
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
到目前为止,我在此站点上看到的此类代码的最佳示例是:
public class StringSplitter {
/* regex was stolen from other Whosebug answer*/
public static void main(String[] args) {
for (String str : "123a4567a8sdfsdfsdgasfsdfsdgsdcvsdfdgdfsdf9".split("(?<=\G.{20})"))
System.out.format("\'%s\'%n", str);
}
}
您可以尝试这样做:
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);
}
}
输出:
Success time: 0.1 memory: [12345, 46512, 31231, 23123,
12365]
我有一个输入字符串到程序,我需要将这个字符串执行到我的方法中,但是如果字符串小于或等于20个字符我想执行它,所以我想拆分这个字符串如果字符串超过 20 个字符,则分成多个字符串。
即输入String的字符数为90个字符则变成5个String 20 + 20 + 20 + 20 + 10 = 90.
我需要每个 20 个字符的字符串和最后一个字符串执行此代码:
try {
enMessage = AES.encrypt(key, message);
sendSMS(contact, enMessage);
} catch (Exception e)
所以它可以让每 20 个字符是一条消息。
您可以使用String的substring方法来拆分您想要的字符串。
您可以在此处了解如何使用它
试试这个:
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
到目前为止,我在此站点上看到的此类代码的最佳示例是:
public class StringSplitter {
/* regex was stolen from other Whosebug answer*/
public static void main(String[] args) {
for (String str : "123a4567a8sdfsdfsdgasfsdfsdgsdcvsdfdgdfsdf9".split("(?<=\G.{20})"))
System.out.format("\'%s\'%n", str);
}
}
您可以尝试这样做:
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);
}
}
输出:
Success time: 0.1 memory: [12345, 46512, 31231, 23123, 12365]