Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Split text if it is longer than 640 characters

Hey there,

so I have written a code that returns a string which. What I want to do is to split it if the string is longer than 640 characters.. I have made three different routes depending on what I want to get returned. The data is collected from the pre written JSON.

If you think you can help me figure this out I would be glad ??

Please PM me or post bellow if you can help me with that. I will share the code privately.

Thanks in advance :)
[Image: GUtlT3Q.jpg]
Code
String data = ...
int maxLength = 640;
ArrayList<String> splitStrings = new ArrayList<String>(); //Strings split at 640 chars are stored here

while(data.length >= maxLength){ //loop
splitStrings.add(s.substring(0, maxLength)); //Get first part of String with length 640
data = s.substring(maxLength); //Set data to the second part
}
Something like this? (Didn't test it but should work)
(Apr 15, 2017 02:45 PM)Weco Wrote:  
Code
String data = ...
int maxLength = 640;
ArrayList<String> splitStrings = new ArrayList<String>(); //Strings split at 640 chars are stored here

while(data.length >= maxLength){ //loop
splitStrings.add(s.substring(0, maxLength)); //Get first part of String with length 640
data = s.substring(maxLength); //Set data to the second part
}
Something like this? (Didn't test it but should work)
Hey there,

thanks for helping tho. But I don't think it will work..

The function returns a string which is XXX characters longs. Depending on a JSON data:
Code
function toMessage(item) {
    return `${item.Title}: ${item.Description} Updated: ${item.Updated}`;
}

I want to split it into 2 or more separate messages if it is longer than 640 characters.
[Image: GUtlT3Q.jpg]
(Apr 15, 2017 03:00 PM)beluga13 Wrote:  Hey there,

thanks for helping tho. But I don't think it will work..

The function returns a string which is XXX characters longs. Depending on a JSON data:
Code
function toMessage(item) {
    return `${item.Title}: ${item.Description} Updated: ${item.Updated}`;
}

I want to split it into 2 or more separate messages if it is longer than 640 characters.
Did you maybe confuse Java and JavaScript? 😊
(Apr 15, 2017 03:39 PM)Weco Wrote:  Did you maybe confuse Java and JavaScript? 😊
Yes probably.. Code is made in JavaScript ?

(Apr 15, 2017 03:39 PM)Weco Wrote:  Did you maybe confuse Java and JavaScript? 😊
Yes probably.. Code is made in JavaScript ?
[Image: GUtlT3Q.jpg]
Assuming javascript, here is my suggestion:

Code
var str = 'abcdefghij',
    msgs = [];

while (str.length) {
    msgs.push(str.substring(0, 3));
    str = str.substring(3);
}

It uses a length of 3 for demonstration and considers remainders.
(Apr 16, 2017 03:43 AM)beluga13 Wrote:  Yes probably.. Code is made in JavaScript ?

Yes probably.. Code is made in JavaScript ?
This section is for Java and not JavaScript​ (completely different languages)
Hmm, try something like this, I'll write this code without IDE so bare with my indentation:

Code
Scanner in = new Scanner(System.in);
System.out.print("Enter your string: ");
String input = in.nextLine();
int printedChars = 0;

if (input.length() > 640){
for (int x = 0; x < input.length(); x++){
if (printedChars == 640){
System.out.println();
printedChars = 0;
}
System.out.print(input.charAt(x));
printedChars++;
}
}

OK just saw that you mistook Java and JS, here is JS version:

Code
// var string = prompt("Enter your string:");  <- random string that user enters

var printedChars = 0;

if (string.length > 640){
for (var x = 0; x < string.length; x++){
if (printedChars === 640){
console.log("\n")
printedChars = 0;
}
console.log(string[x]);
printedChars++;
}
}
Quick Edit: If you want to print first 640 chars on the same line, look here: https://developer.mozilla.org/en-US/docs.../substring
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
I havn't messed around with the code, but maybe do a method or something that keeps on repeating it, so if you have 1300 it checks it twice and splits it into three


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by Mr. RobotApr 30, 2017 03:49 PM 2 188 Apr 30, 2017 03:49 PM
Last Post: Mr. Robot
[.NET] by AρεϰJan 27, 2015 08:09 PM 9 266 Jan 27, 2015 08:09 PM
Last Post: Poppy
[.NET] by NigerianKingJan 26, 2015 04:27 PM 8 235 Jan 26, 2015 04:27 PM
Last Post: Hawk HF