Hack Forums
[Java] Split text if it is longer than 640 characters - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Split text if it is longer than 640 characters (/showthread.php?tid=5600544)



Split text if it is longer than 640 characters - beluga13 - Apr 15, 2017

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 :)


* - Weco - Apr 15, 2017

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)


* - beluga13 - Apr 15, 2017

(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.


* - Weco - Apr 15, 2017

(Apr 15, 2017 03:00 PM)Blaz 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? 😊


* - beluga13 - Apr 16, 2017

(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 ?


* - Owl - Apr 16, 2017

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.


* - Weco - Apr 16, 2017

(Apr 16, 2017 03:43 AM)Blaz 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)


* - SIM-429621 - Apr 16, 2017

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/Web/JavaScript/Reference/Global_Objects/String/substring


* - Acteda - Apr 19, 2017

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