Java拆分字符串并将两部分字符串存储到arraylist中

时间:2018-06-06 04:05:06

标签: java string split

这是一些数据。我想将这些数据分成两部分。第一个是年份(例如913),第二个是关于该特定年份的信息。然后我想将这些数据存储在树形图中作为键值对。我试图使用分割功能做这件事,但它没有给我满意的结果。所以请指导我完成这个。提前谢谢。

这是我的代码。

String open = "event";                                                                       
String close = "birth;
String text = "";
int start = data.indexOf(open);
if (start != -1)
{
     int end = data.indexOf(close, start + open.length());
      if (end != -1)
     {
              text = data.substring(start + open.length(), end);
               text = text.replace("==","");
            String[] words = text.split("–");
               for(String w : words)
                {
                        Log.d("trace w", w.trim());
                }
      }                                                                                                       
}


AD 70 – Emperor Alexander III dies of exhaustion while playing the game tzykanion (Byzantine name for polo). He is succeeded by his 8-year-old nephew Constantine VII.
1513 – Italian Wars: Battle of Novara. Swiss troops defeat the French under Louis II de la Trémoille, forcing the French to abandon Milan. Duke Massimiliano Sforza is restored.
1523 – Gustav Vasa, the Swedish regent, is elected King of Sweden, marking a symbolic end to the Kalmar Union. This is the Swedish national day.
1586 – Francis Drake's forces raid St. Augustine in Spanish Florida.
1644 – The Qing dynasty Manchu forces led by the Shunzhi Emperor capture Beijing during the collapse of the Ming dynasty.
1654 – Queen Christina abdicates the Swedish throne and is succeeded by her cousin Charles X Gustav.
1674 – Shivaji, founder of the Maratha Empire, is crowned.
1749 – The Conspiracy of the Slaves in Malta is discovered.
1762 – Seven Years' War: British forces begin a siege of Havana, Cuba, and temporarily capture the city in the Battle of Havana.
1808 – Napoleon's brother, Joseph Bonaparte, is crowned King of Spain.
1809 – Sweden promulgates a new Constitution, which restores political power to the Riksdag of the Estates after 20 years of enlightened absolutism. At the same time, Charles XIII is elected to succeed Gustav IV Adolf as King of Sweden.
1813 – War of 1812: Battle of Stoney Creek: A British force of 700 under John Vincent defeats an American force twice its size under William Winder and John Chandler.
1822 – Alexis St. Martin is accidentally shot in the stomach, leading to William Beaumont's studies on digestion.
1832 – The June Rebellion in Paris is put down by the National Guard.
1844 – The Young Men's Christian Association (YMCA) is founded in London.
1844 – The Glaciarium, the world's first mechanically frozen ice rink, opens.

5 个答案:

答案 0 :(得分:0)

如果您的数据格式有些不变,您可以找到" - "的第一个实例。从那里你可以轻松地对每一行进行子串。

像这样:

String s = "AD 70 – Emperor Al... Constantine VII.";
String y = s.substring(0, s.indexOf('–')).trim();
String info = s.substring(s.indexOf('–') + 1).trim();

如果以递归方式执行,则可以将键和值添加到树形图中。

不确定这是否是最优雅的解决方案,但它确实有效。

一个。

答案 1 :(得分:0)

ArrayList<String> list= Arrays.asList(Your_String.split("delimiter"));

答案 2 :(得分:0)

以下是您可以遵循以达到上述要求的步骤。

  1. 用“ - ”分割字符串。
  2. 存储到树形图集合中。
  3. 以下是示例。

     public static void convertSplitStringIntoTreeMap() {
        List<String> listOfString = new ArrayList<String>();
        String str = "2012 - This is for testing";
        String str2 = "2013 - This is for testing2";
        listOfString.add(str);
        listOfString.add(str2);
    
        TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();
    
        for (String st : listOfString) {
            String[] splitString = st.split("-");
            tmap.put(Integer.parseInt(splitString[0].trim()), splitString[1]);
        }
    
        System.out.println(tmap);
    }
    

答案 3 :(得分:0)

java 8替代方案:
List<String> list= Stream.of(yourString.split("–")).collect(Collectors.toList());

答案 4 :(得分:0)

我会从您的数据中使用扫描仪。

    Scanner dataInput = new Scanner(data);
    Map<String, String> dataMap = new HashMap<>();

    String input = dataInput.nextLine();
    while (!input.isEmpty()) {
        String[] spliting = input.split("(?=–)", 2);
        dataMap.put(spliting[0], spliting[1]);
        input = dataInput.nextLine();
    } 

    dataMap.forEach((k,v) -> System.out.println(k + v));

该值将保留在 - 。