简体   繁体   English

将字符串分成两个单独的部分

[英]Splitting string into two separate

Well what I meant with that, I create variable with type String and then when I load file, I have to save content in one variable in which case that variable is "TextFromFile", after loading it, I wanted to split it into two separate one and save it in array, after that I just wanted to display two separate strings on screen. 好吧,我的意思是,我创建了一个String类型的变量,然后在加载文件时,我必须将内容保存在一个变量中,在这种情况下,该变量是“ TextFromFile”,加载后,我想将其分为两个一个并保存在数组中,之后我只想在屏幕上显示两个单独的字符串。 That`s the idea. 那是个主意。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package zadania1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Zadania1 
{
    public static void main(String[] args) 
    {
        String TextFromFile = "";
        Zadania1 zad = new Zadania1(); 
        TextFromFile = zad.NacitajObsahZoSubora("C:\\Users\\Ivan\\Desktop\\test.txt");
        String[] pole = TextFromFile.split("[ \\ ]");
        System.out.println("Frist matrica " + pole[0] +" Second matrica: " + pole[1] +"");
    }

    public String NacitajObsahZoSubora(String fileName)
    {
        String text = "", tmp;
        try 
        {
            FileInputStream file = new FileInputStream(fileName);
            InputStreamReader input = new InputStreamReader(file);
            try (BufferedReader fromFile = new BufferedReader(input)) 
            {
                while((tmp = fromFile.readLine()) != null)
                {
                    text = text + "\n" + tmp;
                }
                System.out.println("Obsah suboru:\n" + text);
            }
        }
        catch(IOException e)
        {            
        }
        return text;
    }

    public void ZapisObsahDoSubora(String fileName, String writeText)
    {
        try
        {
            FileOutputStream file = new FileOutputStream(fileName);
            OutputStreamWriter output = new OutputStreamWriter(file);
            try (PrintWriter toFile = new PrintWriter(output)) 
            {
                toFile.println("" + writeText);
            }
        }
        catch (IOException e)
        {   
        }
    }
}

Only this part is showing me that array is out of bounce: 只有这一部分向我展示数组超出了反弹范围:

String[] pole = TextFromFile.split("[ \\ ]");
        System.out.println("Frist matrica " + pole[0] +" Second matrica: " + pole[1] +"");

Error: 错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at zadania1.Zadania1.main(Zadania1.java:24) 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:zadania1.Zadania1.main(Zadania1.java:24)为1

According to what I understood from the comments, assuming your input is in the form of: 根据我从评论中了解的内容,假设您的输入形式为:

[1,2,3;4,5,6;][9,8;7,-6;50,61;]

You need to use another regex in your split, which involves a look-behind: 您需要在拆分中使用另一个正则表达式,这涉及到一个回顾:

String input = "[1,2,3;4,5,6;][9,8;7,-6;50,61;]";
String[] pole = input.split("(?<=\\])");
System.out.println("Frist matrica " + pole[0] +" Second matrica: " + pole[1] +"");

// Output:
// Frist matrica [1,2,3;4,5,6;] Second matrica: [9,8;7,-6;50,61;]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM