有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将字符串拆分为几个不同的字符串

我从一个站点获取源代码,并将代码放入一个字符串中,结果如下所示:

501252,110,34496
331550,30,14114
403186,1,18
325033,31,15750
460287,14,2384
286659,11,1366
419439,1,67
678464,1,0
505044,1,70
522192,1,75
454391,1,0
504858,1,20
505396,1,40
469927,1,0
336670,2,155
392887,5,437
403568,1,0
488324,1,0
524031,1,0
429226,1,0
389668,1,0
383021,1,0
384599,1,0
363131,1,0
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1  

我想拆分每个数字,并将它们保存在自己的字符串中,我该怎么做呢

当前代码:(如果有帮助的话?)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class dfgdfg {

    static String username;
    static JTextField USERNAME = new JTextField();
    static String pooo;
    static JPanel panel;
    static JFrame jframe;
    static JButton button;
    static String line;


    public static void main(String args[]) throws Exception {
GUI();
    }

    public static void getSource() {
        URL url;
        InputStream is = null;
        BufferedReader br;

        try {
            url = new URL(
                    "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player="+username);
            is = url.openStream(); // throws an IOException
            br = new BufferedReader(new InputStreamReader(is));

            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }
        }
    public static void GUI() {
        jframe = new JFrame("HighscoreLookup");
        panel = new JPanel();
        USERNAME = new JTextField("Enter Username");
        button = new JButton("START");

        jframe.setPreferredSize(new Dimension(300, 300));
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
        jframe.add(panel);

        panel.add(USERNAME);
        panel.add(button);
        panel.setBackground(Color.CYAN);
        button.setVisible(true);
        USERNAME.setSize(100,50);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                username = USERNAME.getText().toString();
                System.out.println(username);
                getSource();

            }
        });   
        jframe.pack();
    }

}

共 (5) 个答案

  1. # 1 楼答案

    使用string类的Split方法:

    "data1,data2".Split(',');
    //will return an array containing 2strings: data1 then data2
    

    您可以使用拆分整个内容,然后使用拆分每一行, 或者用扫描仪或缓冲读取器逐行阅读

  2. # 2 楼答案

    "501252,110,34496".split(",") // => ["501252", "110", "34496"]
    
  3. # 3 楼答案

    Scanner读取文件中的每一行,并在每一行上使用String.split(",")。非常实用

    String.split将使用提供的regex语句将字符串拆分为1D数组。在这里,你只需要用每个逗号打断每一行

    编辑:

    根据你的评论,这仍然对你有用

    给定输入"501252,110,34496"输出将是"501252", "110", "34496"

    运行以下代码以了解我的意思:

    public static void main(String[] args){
        String input = "501252,110,34496";
        String output = input.split(",");
        String[] outputContainsThis = new String[]{"501252", "110", "34496"};
    }
    

    这够了吗

  4. # 4 楼答案

    你可以使用char[] cArray = mystring.toCharArray();

  5. # 5 楼答案

    有很多方法可以做到这一点。最简单的方法是使用split:

    String inputString = "line 1\nline 2\n";
    
    StringBuilder sb = new StringBuilder();
    
    String[] lines = inputString.split("\\n");
    
    for(String line : lines)
    {
        System.out.println(line);
    }
    

    我大致是这样做的,因为它允许简单的错误处理、字符转义和其他处理:

    List<String> lines = new ArrayList<String>();
    
    for(int i = 0; i < inputString.length(); i++)
    {
        char next = inputString.charAt(i);
    
        if(next == '\\')
        {
            i++;
            continue;
        }
    
        if(next == '\n')
        {
            lines.add(sb.toString());
            sb = new StringBuilder();
            continue;
        }
    
        sb.append(next);
    }
    
    if(sb.length() > 0)
    {
        lines.add(sb.toString());
    }
    
    for(String line : lines)
    {
        System.out.println(line);
    }
    

    更新:问题从行改为字段。您仍然可以使用split,但要注意特殊字符、新行字符等。处理此类输入的正确方法是使用CSV解析器来处理输入(请参阅RFC 4180:http://www.ietf.org/rfc/rfc4180.txt