【问题标题】:How to Split a String into Different Variables如何将字符串拆分为不同的变量
【发布时间】:2013-04-03 05:06:04
【问题描述】:

假设我有以下代码:

String s = "{U1,U2,U3},{U5,U7},{U4,U6,U8}";

我怎样才能让它看起来像下面?

String s1 = {U1,U2,U3};
String s2 = {U5,U7};
String s3 = {U4,U6,U8};

s 中的组合可以是任何形式。 s1, s2, s3 是不同的字符串。

【问题讨论】:

    标签: java parsing


    【解决方案1】:

    您可以使用以下方法:

    example:

      public static void parse(String[] args) throws java.lang.Exception
      {
         String myString = "{U1,U2,U3},{U5,U7},{U4,U6,U8}";
         int begin = 0;
         int end = 0;
         String s1;
         while (end != -1){     
           end = myString.indexOf("},{", begin);
    
           if ((end < myString.length()) && ((begin < end)))
             s1 = myString.substring(begin, end + 1);
           else
             s1 = myString.substring(begin);
    
           begin = end + 2;
           System.out.println(s1);
         }
      }
    

    【讨论】:

    • 嗨..我想要不同变量中的每个子字符串,例如 s1={U1,U2,U3} s2={U5,U7} s3={U4,U6,U8}跨度>
    • SO 不是一个给出运行代码的站点,您需要付出一些努力,尝试修改代码并询问您是否遇到问题。
    【解决方案2】:

    这是我的代码:

    public class SplitString {
        public static void main(String[] args) {
    
            String s ="{U1,U2,U3},{U5,U7},{U4,U6,U8}";
            String[] splitted = s.split("},");
    
            // add the end brace for every entry except the last
            for (int i=0 ; i < splitted.length-1 ; i++) {
                splitted[i]=splitted[i] + "}";
            }
    
            // print out the string array
            for (int i=0; i< splitted.length ; i++) {
                System.out.println("String s"+i+" = "+splitted[i]);
            }
        }
    }
    

    每次遇到},这两个字符时都会拆分,将其放入“拆分”的字符串数组中,然后循环遍历字符串数组并在除最后一个之外的每个字符的末尾添加}

    输出:

    String s0 = {U1,U2,U3}
    String s1 = {U5,U7}
    String s2 = {U4,U6,U8}
    

    【讨论】:

      【解决方案3】:

      通过使用replace()函数将"{"替换为" "并基于","进行拆分;将这些值放入数组并对数组进行排序。现在您可以轻松地显示您想要的任何格式。

      【讨论】:

        【解决方案4】:

        拆分,} 字符组合 .以下代码在 C# 中。我不知道确切的 Java 等价物。

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Text.RegularExpressions;
        
        namespace ConsoleApplication1
        {
            class Program
            {
                static void Main(string[] args)
                {
                    string s = "{U1,U2,U3},{U5,U7},{U4,U6,U8}";
        
                    string[] splitstring = Regex.Split(s,"},");
                    // the string obtained are splitstring[0]= {U1,U2,U3 splitstring[1]={U5,U7    
                    // splitstring[2]={U4,U6,U8}
        
                   // Note only last string is not missing closing bracket so in a loop we append closing                 
                  //  bracket to all strings except last string
        
                    for (int i = 0; i < splitstring.Length; i++)
                        if (i == splitstring.Length - 1)
                            continue;
                        else
                            splitstring[i] = splitstring[i] + "}";
        
                    foreach (string str in splitstring)
                        Console.WriteLine("\n" + str);
                }
            }
        }
        

        【讨论】:

        • 请在回答 Java 问题时提供 Java 代码。
        • 不懂Java的可以给个算法/idea/psuedocode
        【解决方案5】:
        public static void main(String... args) {
        
            String input = "{U1,U2,U3},{U5,U7},{U4,U6,U8}";
        
            Pattern pattern = Pattern.compile("\\{[^\\}]*\\}");
            Matcher matcher = pattern.matcher(input);
            while (matcher.find()) {
                String s = matcher.group();
                System.out.format("%s\n", s);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-08-11
          • 2015-11-30
          • 2019-02-05
          • 1970-01-01
          • 2021-10-26
          • 1970-01-01
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多