-1

So I have this string : "New York,USA,1000\n" + "City,World,2000\n";

I need to split it first by newline and then by comma, so at the end I get an array of strings that is like this: New York has index 0, USA index 1, 1000 index 2, World index 4 and so on..I tried using String[] newline = string.split("\n") and then declaring a new array of strings called result ( giving it 1000 characters randomly ) and making a for loop like this:

String[] result = new String[1000];
for (String s : newline){
  result=s.split(",");
}

But it doesn't work properly. Any help, please?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kopite1905
  • 15
  • 2
  • 7

4 Answers4

1

Different operating system has a different new line

Please check https://en.wikipedia.org/wiki/Newline

  • UNIX or Mac \r
  • Windows \r\n

    String[] lines = text.split("\\r?\\n");
    for (String line : lines) {
        System.out.println("line  : " + line);
        String[] words = line.split(",");
        System.out.println("words : " + words);
    }
    
dassum
  • 4,727
  • 2
  • 25
  • 38
1

If you are using java 8 or higher try try something like:

String str =  "New York,USA,1000\n" + "City,World,2000\n";
String[] result = Pattern.compile("\n")
                         .splitAsStream(str)
                         .map(s -> s.split(","))
                         .flatMap(Arrays::stream)
                         .toArray(String[]::new);
Eritrean
  • 15,851
  • 3
  • 22
  • 28
1
public class Test {
    public static void main(String[] args) {
        String string = "New York,USA,1000\n" + "City,World,2000\n";
        String[] newline = string.split("\n");
        String[] result = new String[1000];
        //first loop
        result = newline[0].split(",");
        System.out.println(Arrays.toString(result));
        //second loop
        result = newline[1].split(",");
        System.out.println(Arrays.toString(result));
    }
}
/*
output:
[New York, USA, 1000]
[City, World, 2000]
 */

result would be overrided instead of append.

public class Test {
    public static void main(String[] args) {
        String string = "New York,USA,1000\n" + "City,World,2000\n";
        String[] newline = string.split("\n");
        String[] result = new String[1000];
        int index = 0;
        for (String s : newline) {
            for (String t : s.split(",")) {
                result[index++] = t;
            }
        }
        for (int i = 0; i < index; i++) {
            System.out.print(result[i] + " ");
        }
    }
}
/*
output:
New York USA 1000 City World 2000 
 */
chachay
  • 340
  • 2
  • 8
0

If I get you correctly:

import re

stringobject = "New York,USA,1000\n" + "City,World,2000\n"

emptylist = []
for str_obj in stringobject.splitlines():
    o = str_obj.split(sep=",")
    emptylist.append(o)

print(emptylist)

[['New York', 'USA', '1000'], ['City', 'World', '2000']]

Which is a list of list and can be accessed:

print(emptylist[0][0])
'New York'
JA-pythonista
  • 1,225
  • 1
  • 21
  • 44