简体   繁体   English

从字符串获取子字符串(我从文本文件获取字符串)

[英]getting substrings from a string (i get the string from a text file)

I'm trying to put my messages in a chat to make something look like this: 我正在尝试将我的消息聊天,以使事情看起来像这样:

在此处输入图片说明

This is my String of messages: 这是我的消息字符串:

String messages = "Me said: Hola\n"+
                  "Steven said: Hola android client\n"+
                  "Me said: como estas ?\n"+
                  "Steven said: Yo muy bien y tu?\n"+
                  "Me said: bueno si esto funciona me sentire magnifico ;)\n"+
                 "Me said: ah cierto me olvidaba veamos si dos mensajes seguidos funcionan\n"+
                 "Steven said: bien\nVeamos si funciona\n"+
                 "Me said: se xd\n";

I tried this to make it read each line: 我试图这样做,以使其读取每一行:

    while(messagesToRead.contains("Me said:") || messagesToRead.contains("Steven said:")){
        if(messagesToRead.indexOf("Me said:")==0)
          {
            // 8 because 'Me said:' size is 8 , -1 because i don't want to read the last  \n
            String messageToAdd=messagesToRead.substring(8,(messagesToRead.indexOf("Steven said:"))-1);
            // if the first @param is false i set it to right
            adapter.add(new DataProvider(false,messageToAdd));
            messagesToRead = messagesToRead.substring(messageToAdd.length()+9);
          }
         else if(messagesToRead.indexOf("Steven said:")== 0)
          {
            // 12 because of 'Steven said:' size
            String messageToAdd=messagesToRead.substring(12,messagesToRead.indexOf("Me said:")-1);
            adapter.add(new DataProvider(true,messageToAdd));
            messagesToRead = messagesToRead.substring(messageToAdd.length()+13);
           } 

        }

I know I have a problem with the logic, and it is not the best way to read each line but I just wanted to make it work with that way for now. 我知道我在逻辑上有问题,这不是读取每一行的最佳方法,但我现在只是想使其以这种方式工作。

This should work. 这应该工作。 I just tested it. 我刚刚测试过。

    String messagesToRead = "Me said: Hola\n"+
                    "Steven said: Hola android client\n"+
                    "Me said: como estas ?\n"+
                    "Steven said: Yo muy bien y tu?\n"+
                    "Me said: bueno si esto funciona me sentire magnifico ;)\n"+
                   "Me said: ah cierto me olvidaba veamos si dos mensajes seguidos funcionan\n"+
                   "Steven said: bien\nVeamos si funciona\n"+
                   "Me said: se xd\n";

            String[] messages= messagesToRead.split("\n");
            ArrayList<String> arr = new ArrayList<String>(Arrays.asList(messages));
            messages=null;
            String pattern1 = "Me said:";
            String pattern2 = "Steven said:";
            int size =  arr.size(); //you need to have the initial size before processing
             // lines you are talking about
             for(int i=size-1;i>=0;i--)
             {
                  String str = arr.get(i);
                  if(str!="" && str.indexOf(pattern1)==-1 && str.indexOf(pattern2)==-1)
                  {
                     String tmp = arr.remove(i);
                     String old =arr.get(i-1) ;
                     arr.set(i-1,old+"\n"+tmp);
                  }
             } 

            messages = new String[arr.size()];
            messages = arr.toArray(messages);

            for(String mess : messages)
            {
                if(mess!="" && mess.indexOf(pattern2) == 0)
                {
                     /*code here*/
                    System.out.println(mess);
                }
            }

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

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