java - Android:如何用多行长的项目拆分一个很长的字符串?

标签 java android string arraylist split

我有一个很长的字符串,它来 self 的应用从互联网下载的文件。现在,在我将文件转换为字符串后,我使用 String.split 拆分字符串,然后遍历文件,将我拆分的内容转换为字符串( fragment ),字符串“Description”通常包含多行。之后,我将这些字符串放入自定义数组列表中,然后使用自定义适配器显示放入数组列表中的每个项目,除了它只显示第一行描述。我怎样才能让它显示“描述”中的所有行?

如何使 read.line 将“Description”部分视为一行,或者是否有某种方法可以使“Description”中的行始终合并?

这是我要解析的字符串的内容:

"","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","","" "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","","" "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","","" "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","","" "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","","" "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework","

Hello chemistry students,



We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.


Note:

The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:
Acids donate protons (H+)

Bases accept protons (H+)

The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states:

Acids produce protons (H+)

Bases produce hydroxide ions (OH-)

Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt.

Neutrally,

Mr. Mott

" "","SXGPW","1","PBAT Science","","","","","" "","MXGRW","1","PBAT Algebra 2/Trig ","","","","","" "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","","" "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework","

 1. The last page of your PBA review packets both readings.

ANSWER QUESTIONS IN COMPLETE SENTENCES.

" "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework","

 For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.

Have a great night!

" "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework","

 A.  pg. 803 #24,25,28,30,31

B.  Check your answers here.

www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf

C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 

"

编辑:我发现 read.line 实际上每次读取新行时都会再次启动 while 循环,但它会记住它所在的变量,因此每次遇到时都会创建一个全新的“Description”字符串一个新行(并且新行仅在字符串的“描述”部分)并且被添加到自定义数组列表中。所以我需要弄清楚如何制作它,这样它就不会认为描述被分成不同的行。

编辑:我认为使用 Scanner 可以解决我的问题,但是如果我在 while 循环中使用扫描仪,它不会在每次循环时从它停止的地方继续,并且在完成时不会自行关闭文件。

我尝试过的事情:

  • 将字符串“Description”全部写成一行(然后实际的解析器什么都不返回)
  • 将 .trim() 添加到末尾(什么都不做)
  • 删除了“检查“Type”是否为空”,它添加了一些 ListView 部分,其中包含字符串“Description”的其他行

这是实际解析器的代码

Log.d("homework due today", Due_Today);


            InputStream is = new ByteArrayInputStream(Due_Today.getBytes());


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));



            try {

                while ((Due_Today = reader.readLine()) != null) {   
            String[] part = Due_Today.split("\",\"", -1);
            int noOfItems = part.length;
            int counter=0;
            Description = counter<noOfItems ? part[counter] : "";
            counter++;
            Band = counter<noOfItems ? part[counter] : "";
            counter++;
            Number = counter<noOfItems ? part[counter] : "";
            counter++;
            Class = counter<noOfItems ? part[counter] : "";
            counter++;
            Teacher = counter<noOfItems ? part[counter] : "";
            counter++;
            Title = counter<noOfItems ? part[counter] : "";
            counter++;
            Date = counter<noOfItems ? part[counter] : "";
            counter++;
            Type = counter<noOfItems ? part[counter] : "";
            counter++;
            Data = counter<noOfItems ? part[counter] : "";
            counter++; 


            Description = Description.replaceAll("^\"|\"$", "");
            Band = Band.replaceAll("^\"|\"$", "");
            Number = Number.replaceAll("^\"|\"$", "");
            Class = Class.replaceAll("^\"|\"$", "");
            Teacher = Teacher.replaceAll("^\"|\"$", "");
            Title = Title.replaceAll("^\"|\"$", "");
            Date = Date.replaceAll("^\"|\"$", "");
            Type = Type.replaceAll("^\"|\"$", "");
            Data = Data.replaceAll("^\"|\"$", "");






            Log.d("Data", Data);
            Log.d("Band", Band);
            Log.d("Number", Number);
            Log.d("Class", Class);
            Log.d("Teacher", Teacher);
            Log.d("Title", Title);
            Log.d("Date", Date);
            Log.d("Type", Type);
            Log.d("Description", Description);



            due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description));

自定义适配器代码

private void populateListView() {
        ArrayAdapter<Due_Today_List> adapter = new Due_TodayAdapter();
        ListView list = (ListView)getView().findViewById(R.id.listView1);
        list.setAdapter(adapter);




    }


    public class Due_TodayAdapter extends ArrayAdapter<Due_Today_List>  {
        public Due_TodayAdapter() {
            super(getActivity(), R.layout.item_view, due_today_list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;




            if(convertView==null){
            convertView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
             holder = new ViewHolder();



             holder.imageView = (ImageView)convertView.findViewById(R.id.item_iconclass);



             holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.item_texthomeworkdue);

            holder.DescriptionText = (TextView) convertView.findViewById(R.id.item_textdescription);

            holder.TeacherText = (TextView) convertView.findViewById(R.id.item_textteacher);

            holder.TypeText = (TextView) convertView.findViewById(R.id.item_texttype);

            convertView.setTag(holder);


            }

        else {
            holder = (ViewHolder) convertView.getTag();
        }



        Due_Today_List currenthomeworkdue = due_today_list.get(position);

            if(currenthomeworkdue.getType() != null && !currenthomeworkdue.getType().isEmpty()){






        Teacher = currenthomeworkdue.getTeacher().substring(0,1).toUpperCase()+currenthomeworkdue.getTeacher().substring(1).toLowerCase();

        Description = currenthomeworkdue.getDescription().substring(5); 



        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("S")){

        holder.imageView.setImageResource(R.drawable.science);


        }


        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("M")){

            holder.imageView.setImageResource(R.drawable.mathematics);


            }


        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("H")){

            holder.imageView.setImageResource(R.drawable.global_studies);


            }

        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 2)).equals("FS")){

            holder.imageView.setImageResource(R.drawable.spanish);


            }

        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("E")){

            holder.imageView.setImageResource(R.drawable.english);


            }

        holder.HomeworkDueText.setText(currenthomeworkdue.getTitle());

        holder.DescriptionText.setText(Description);


        holder.TeacherText.setText(Teacher);

        holder.TypeText.setText(currenthomeworkdue.getType());

        return convertView;

            } else {


                View empty = getActivity().getLayoutInflater().inflate(R.layout.empty_item, parent, false);



       return empty;






        }

        }

这是日志返回的内容

07-21 21:33:14.704: D/homework due today(8127): "","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): Hello chemistry students,
07-21 21:33:14.704: D/homework due today(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.
07-21 21:33:14.704: D/homework due today(8127): Note:
07-21 21:33:14.704: D/homework due today(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  
07-21 21:33:14.704: D/homework due today(8127): Acids donate protons (H+)
07-21 21:33:14.704: D/homework due today(8127): Bases accept protons (H+)
07-21 21:33:14.704: D/homework due today(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 
07-21 21:33:14.704: D/homework due today(8127): Acids produce protons (H+)
07-21 21:33:14.704: D/homework due today(8127): Bases produce hydroxide ions (OH-)
07-21 21:33:14.704: D/homework due today(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): Neutrally,
07-21 21:33:14.704: D/homework due today(8127): Mr. Mott
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "","SXGPW","1","PBAT Science","","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","MXGRW","1","PBAT Algebra 2/Trig ","","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","",""
07-21 21:33:14.704: D/homework due today(8127): "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  1. The last page of your PBA review packets both readings.
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES.
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.
07-21 21:33:14.704: D/homework due today(8127): Have a great night!
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  A.  pg. 803 #24,25,28,30,31
07-21 21:33:14.704: D/homework due today(8127): B.  Check your answers here.
07-21 21:33:14.704: D/homework due today(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf
07-21 21:33:14.704: D/homework due today(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127):     
07-21 21:33:14.714: D/Band(8127): ZQ11ISF1
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): INSTRUCTIONAL SKILLS
07-21 21:33:14.714: D/Teacher(8127): MARKOVIC
07-21 21:33:14.714: D/Band(8127): TQS11QP2
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): COMPUTER PROGRAMMING 2
07-21 21:33:14.714: D/Teacher(8127): THORP
07-21 21:33:14.714: D/Band(8127): ZLF
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): FRIDAY LUNCH
07-21 21:33:14.714: D/Teacher(8127): MARKOVIC
07-21 21:33:14.714: D/Band(8127): PHS84
07-21 21:33:14.724: D/Number(8127): 17
07-21 21:33:14.724: D/Class(8127): HEALTH & WELLNESS TERM 4 OF 8
07-21 21:33:14.724: D/Teacher(8127): REED
07-21 21:33:14.724: D/Band(8127): PQS11QPI
07-21 21:33:14.724: D/Number(8127): 1
07-21 21:33:14.724: D/Class(8127): INDV. PHYS ED
07-21 21:33:14.724: D/Teacher(8127): MARKOVIC
07-21 21:33:14.724: D/Band(8127): SCS22
07-21 21:33:14.724: D/Number(8127): 1
07-21 21:33:14.724: D/Class(8127): CHEMISTRY 2 OF 2
07-21 21:33:14.734: D/Teacher(8127): Mott
07-21 21:33:14.734: D/Title(8127): Chemistry HW for tomorrow
07-21 21:33:14.734: D/Date(8127): 2014-06-03
07-21 21:33:14.734: D/Type(8127): Homework
07-21 21:33:14.734: D/Description(8127): 18342
07-21 21:33:14.734: D/Description(8127):  
07-21 21:33:14.744: D/Description(8127):  
07-21 21:33:14.754: D/Description(8127): Hello chemistry students,
07-21 21:33:14.754: D/Description(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.
07-21 21:33:14.764: D/Description(8127): Note:
07-21 21:33:14.774: D/Description(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  
07-21 21:33:14.774: D/Description(8127): Acids donate protons (H+)
07-21 21:33:14.784: D/Description(8127): Bases accept protons (H+)
07-21 21:33:14.784: D/Description(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 
07-21 21:33:14.794: D/Description(8127): Acids produce protons (H+)
07-21 21:33:14.804: D/Description(8127): Bases produce hydroxide ions (OH-)
07-21 21:33:14.804: D/Description(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 
07-21 21:33:14.804: D/Description(8127):  
07-21 21:33:14.814: D/Description(8127): Neutrally,
07-21 21:33:14.814: D/Description(8127): Mr. Mott
07-21 21:33:14.824: D/Description(8127):  
07-21 21:33:14.824: D/Band(8127): SXGPW
07-21 21:33:14.824: D/Number(8127): 1
07-21 21:33:14.824: D/Class(8127): PBAT Science
07-21 21:33:14.824: D/Band(8127): MXGRW
07-21 21:33:14.824: D/Number(8127): 1
07-21 21:33:14.824: D/Class(8127): PBAT Algebra 2/Trig 
07-21 21:33:14.824: D/Band(8127): EES84
07-21 21:33:14.824: D/Number(8127): 4
07-21 21:33:14.824: D/Class(8127): ENGLISH 10 2 OF 2
07-21 21:33:14.824: D/Teacher(8127): GURALNICK
07-21 21:33:14.824: D/Band(8127): FSS84
07-21 21:33:14.824: D/Number(8127): 8
07-21 21:33:14.824: D/Class(8127): SPANISH II 2 OF 2
07-21 21:33:14.824: D/Teacher(8127): RATCLIFFE
07-21 21:33:14.824: D/Title(8127): Tarea para 6/3
07-21 21:33:14.824: D/Date(8127): 2014-06-03
07-21 21:33:14.824: D/Type(8127): Homework
07-21 21:33:14.824: D/Description(8127): 18265
07-21 21:33:14.834: D/Description(8127):  1. The last page of your PBA review packets both readings.
07-21 21:33:14.834: D/Description(8127):  
07-21 21:33:14.834: D/Description(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES.
07-21 21:33:14.844: D/Band(8127): HGS44
07-21 21:33:14.844: D/Number(8127): 10
07-21 21:33:14.844: D/Class(8127): GLOBAL STUDIES 10 2 OF 2
07-21 21:33:14.844: D/Teacher(8127): Anderson
07-21 21:33:14.844: D/Title(8127): Intro Paragraph and EDAC
07-21 21:33:14.844: D/Date(8127): 2014-06-03
07-21 21:33:14.844: D/Type(8127): Homework
07-21 21:33:14.844: D/Description(8127): 18413
07-21 21:33:14.844: D/Description(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.
07-21 21:33:14.844: D/Description(8127): Have a great night!
07-21 21:33:14.854: D/Band(8127): MRS22
07-21 21:33:14.854: D/Number(8127): 7
07-21 21:33:14.854: D/Class(8127): ALGEBRA 2 / TRIG 2 OF 2
07-21 21:33:14.854: D/Teacher(8127): KLEIN
07-21 21:33:14.854: D/Title(8127): HW #28
07-21 21:33:14.854: D/Date(8127): 2014-06-03
07-21 21:33:14.854: D/Type(8127): Homework
07-21 21:33:14.854: D/Description(8127): 18330
07-21 21:33:14.854: D/Description(8127):  A.  pg. 803 #24,25,28,30,31
07-21 21:33:14.854: D/Description(8127): B.  Check your answers here.
07-21 21:33:14.854: D/Description(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf
07-21 21:33:14.864: D/Description(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 
07-21 21:33:14.864: D/Description(8127):   

编辑: 所以我试图在解析器 while 循环中添加一个 while 循环,它看起来收集“Description”的所有行并将它们全部添加到一个字符串中。我对它的运气并不好,因为它似乎从来没有做我想让它做的事。它跳过文件的所有第一部分,这是它应该做的,然后它把“描述”部分放在一起,但在那之后一切都变得疯狂,它把所有这些额外的东西添加到 StringBuilder 中。无论如何,这是新的解析器代码及其在日志中的输出。

StringBuilder DescriptionAll = new StringBuilder();

            InputStream is = new ByteArrayInputStream(Due_Today.getBytes());


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));



            try {

                while ((Due_Today = reader.readLine()) != null) {   
            String[] part = Due_Today.split("\",\"", -1);
            int noOfItems = part.length;
            int counter=0;


            DescriptionCheck = counter<noOfItems ? part[counter] : "";counter++;

            if (!Due_Today.contains("\",\"") && DescriptionCheck != null && !DescriptionCheck.isEmpty() ){

                while ((Due_Today = reader.readLine()) != null & !DescriptionCheck.contains("\",\"")) {



                    Log.d("DescriptionCheck", DescriptionCheck );

                    String[] parter = Due_Today.split(System.getProperty("line.separator"), -1);



                Description = parter[0];

                DescriptionAll.append((System.getProperty("line.separator") + Description));

                Log.d("descriptioncheck", Description );

                if (Description.contains("\",\"")) {


                    Log.d("BROKEN?", "YES" );

                    break;


                }

            }
            }

            Band = counter<noOfItems ? part[counter] : "";
            counter++;
            Number = counter<noOfItems ? part[counter] : "";
            counter++;
            Class = counter<noOfItems ? part[counter] : "";
            counter++;
            Teacher = counter<noOfItems ? part[counter] : "";
            counter++;
            Title = counter<noOfItems ? part[counter] : "";
            counter++;
            Date = counter<noOfItems ? part[counter] : "";
            counter++;
            Type = counter<noOfItems ? part[counter] : "";
            counter++;
            Data = counter<noOfItems ? part[counter] : "";
            counter++; 






            Band = Band.replaceAll("^\"|\"$", "");
            Number = Number.replaceAll("^\"|\"$", "");
            Class = Class.replaceAll("^\"|\"$", "");
            Teacher = Teacher.replaceAll("^\"|\"$", "");
            Title = Title.replaceAll("^\"|\"$", "");
            Date = Date.replaceAll("^\"|\"$", "");
            Type = Type.replaceAll("^\"|\"$", "");
            Data = Data.replaceAll("^\"|\"$", "");






        Description =   DescriptionAll.toString();

        Description = Description.replaceAll("^\"|\"$", "");

            Log.d("Data", Data);
            Log.d("Band", Band);
            Log.d("Number", Number);
            Log.d("Class", Class);
            Log.d("Teacher", Teacher);
            Log.d("Title", Title);
            Log.d("Date", Date);
            Log.d("Type", Type);
            Log.d("Description", Description);
            Log.d("DescriptionAll", Description);




            due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description));

here是日志现在的样子,纯粹的疯狂:http://pastebin.com/M8ZT07ii

编辑:

这是应用程序的屏幕截图,当所有内容都完美排序时,除了“描述”(其中只有每个描述的第一行出现),它使用我尝试的第一个代码(没有新的 while 循环“

enter image description here

而这里你可以看到它只显示了“Description”的第一行

enter image description here

这是我将新代码与 while 循环一起使用时应用程序的样子

enter image description here

下面是使用 while 循环的新代码的“Description”输出,正如您看到的 while 循环的结果,我删除了“Description”的第一行,这没关系,因为它只是一个占位符

enter image description here enter image description here

enter image description here enter image description here

最佳答案

我认为问题出在您使用 readLine 读取文件这一事实。通过使用此函数,缓冲区会被读取,直到找到换行符“\n”或回车符“\r”。但是,您想要的是在每次找到出现的 ',' 时拆分字符串。

尝试使用read 来通读Due_Today。这只是一个伪代码:

int value   = 0;
int counter = 0;
String str  = "";
StringBuilder strb = new StringBuilder();

// Read until the end of the file
while((value = reader.read()) != -1)
{
    // Convert int to a character
    char c = (char)value;
    strb.append(c);

    // If the separator is found
    if (c == ','){

        str = strb.toString().replaceAll("^\"|\"$", "");
        switch (counter) {

            /* Do things according to the value of counter:
            Read description, data, title.. according to the data structure of what you are trying to parse 

            Example:
            case 1:  Description = str;
            break;
            case 2:  Band = str;
            break;
            */

        }

        // Prepare to read a new string and initialize the string builder
        strb.setLength(0);
        counter ++;
    }
}

我确实知道你正在解析的文件的数据结构是什么,但如果你必须读取一个文件中的多个对象,你可能会遇到一些问题。我注意到“描述”可以包含一些逗号,这些逗号将被视为分隔符。因此,您可以只读取一个对象并说“一旦我开始阅读描述,无论是否有逗号,都读取文件直到最后”,或者将分隔符更改为字符串内容中未使用的字符。

希望对你有帮助

编辑

1) 用这样的东西来搜索你的分隔符怎么样?它可能不是完美的方法,但至少速度很快,因为它不会在每个字符处搜索整个分隔符模式。

while((value = reader.read()) != -1)
{
     // Convert int to a character
     char c = (char)value;


      // Searching for the separator
      if (c == ','){
           if (state == 1) {
                state = 2;
           } else {
                state = 0;
                strb.append(c);
            }
       }
       else if (c == '"'){
            if (state == 2){
                 // The separator if found, read the string
                 processElement(strb, counter); 

                  // Prepare to read a new string
                  state = 0;
                  counter ++;

             } else {
                  state = 1;
                  strb.append(c);
             }
       } else {
             strb.append(c);
       }
}

// Process the last element
processElement(strb, counter);


public void processElement(StringBuilder strb, int counter){
    String str = strb.toString().replaceAll("^\"|\"$", "");
    System.out.println("String to process= " + str);

    /*
         Do something with it here..

    switch (counter) {
        case 1:  Description = str;
        break;

        case 2:  Band = str;
        break;

        ...
    }
    */

    // Initialize the string builder
    strb.setLength(0);
}

2)如果你愿意读到文件直到最后,你可以这样做:

while((value = reader.read()) != -1)
{
     // Convert int to a character
     char c = (char)value;
     strb.append(c);
}
String str = strb.toString();

关于java - Android:如何用多行长的项目拆分一个很长的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877673/

相关文章:

正则表达式提取与 R 中某些单词匹配的部分字符串

java - 开始开发用于存储和搜索模型和产品的 Java 应用程序的提示

java - 读取以 [ 开头并以 ] 结尾的 JSON 数据

Android SeekBar 最小和连续浮点值属性

java - 在Java中添加字符串在一起?

python - matplotlib 中的 TeX 渲染、大括号和字符串格式化语法

java - 基于 Java 的 Web 应用程序的分析工具

java - for 循环可以处理随机数吗?

android - 将 Facebook SDK 导入我的 android 项目 - Gradle 错误

Android:如何创建详细警报对话框