Reputation: 105
I'm trying to split a string after character 266 for instance. The user enters a string, and if its over a certain length, spit it at the (in my case) 266th char. Here is my code:
if(inputText.length() > 266){
//Max Chars Reached. Move Some To Part 2
}
So, what I need is that it takes the 267th and on and sets that to a var(p2 for instance) and rest to p1.
Upvotes: 1
Views: 548
Reputation: 702
String p1 = InputText.substring(0, 266);String p2 = InputText.substring(266);
Upvotes: 0
Reputation: 1032
Put it in a loop, sth like this:
String s="......"; //your long string
while (s.length()>266) {
System.out.println(s.substring(0,266)); //process substring
s=s.substring(266); // reloop the rest
}
System.out.println(s); // process last chunk
Upvotes: 0
Reputation: 1171
String splited = inputText.substring(index);
// in your case
String splited = inputText.substring(266);
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)
Upvotes: 0
Reputation: 1480
You can use substring()
to split strings at specific indexes:
if(inputText.length() > 266){
String var1 = inputText.substring(0, 267);
String var2 = inputText.substring(267);
}
More information for Strings can be found in Javadocs.
Upvotes: 4
Reputation: 11440
Use String.substring()
as descriped in the API. This allows to split a String beginning with a certain index or from an start index till an end index. See this simple example:
public static void main(String []args){
String myString = "Hallo";
String part1 = myString.substring(0,2);
String part2 = myString.substring(2);
System.out.println(part1); //Ha
System.out.println(part2); //llo
}
Upvotes: 3