分割字符串以获取最后一个字符出现的位置

huangapple go评论73阅读模式
英文:

Splitting a string for last occurrence a character

问题

我正在尝试使用以下格式拆分字符串:

"abc=cde,dfe=lk,f,sss=f,d,s"

我希望通过使用第一组字符作为键,第二组字符作为值,将这些值恢复到一个映射中。

例如

  • 键:abc,值:cde
  • 键:dfe,值:lk,f
  • 键:sss,值:f,d,s

因此,将这些值拆分为最后一个出现的","。

有关如何执行此操作的任何想法吗?

我尝试过正则表达式和Stringtokenizer,但我无法仅恢复最后一个出现的","。

英文:

I'm trying to split a String with a format like this :

"abc=cde,dfe=lk,f,sss=f,d,s" 

I'd like to recover these values in a map by using the first set of characters as a key and the second ones as value.

For example

  • key: abc, value: cde
  • key: dfe, value: lk,f
  • key: sss, value: f,d,s

So splitting these values for the last occurrence of ",".

Any ideas on how to do it?

I tried with regex and Stringtokenizer but I can't manage to recover just the last occurrence of ","

答案1

得分: 1

你可以使用以下正则表达式(可能可以进行优化):

,(?=(?:(?!,).)*=)

(在Regex101上查看)

这将匹配一个逗号,,该逗号在下一个等号=之前没有后续的逗号,

英文:

You could use the following regex (could possibly be optimized):

,(?=(?:(?!,).)*=)

(see on Regex101)

This matches a , which has no subsequent , until the next =.

答案2

得分: 0

你需要使用正则表达式来完成这个任务。

完整代码

public class Test {
    
    public static void main(String args[]) {
        
        String input = "abc=cde,dfe=lk,f,sss=f,d,s";
        String[] arrOfStr = input.split(",(?=(?:(?!,).)*=)");
        
        HashMap<String, String> properties = new HashMap<String, String>();
        
        for(int i=0;i<arrOfStr.length;i++) {
            String[] temp = arrOfStr[i].split("=");
            properties.put(temp[0], temp[1]);    		
        }        	
        
        System.out.println("Input String     : " + input);
        System.out.println("\nFinal properties : ");
        
        properties.entrySet().forEach(entry->{
            System.out.println("key = " + entry.getKey() + " :: value = " + entry.getValue());  
        });
   
    }    
}

输出

Input String     : abc=cde,dfe=lk,f,sss=f,d,s

Final properties : 
key = dfe :: value = lk,f
key = sss :: value = f,d,s
key = abc :: value = cde
英文:

You need to use regex for this.

Full Code :

public class Test {
    
    public static void main(String args[]) {
    	
    	String input = &quot;abc=cde,dfe=lk,f,sss=f,d,s&quot;;
    	String[] arrOfStr = input.split(&quot;,(?=(?:(?!,).)*=)&quot;);
    	
    	HashMap&lt;String, String&gt; properties = new HashMap&lt;String, String&gt;();
    	
    	for(int i=0;i&lt;arrOfStr.length;i++) {
    		String[] temp = arrOfStr[i].split(&quot;=&quot;);
    		properties.put(temp[0], temp[1]);    		
    		
    	}        	
    	System.out.println(&quot;Input String     : &quot; +input);
	    System.out.println(&quot;\nFinal properties : &quot;);
	
	    properties.entrySet().forEach(entry-&gt;{
	    System.out.println(&quot;key = &quot; +entry.getKey() + &quot; :: value = &quot; + entry.getValue());  
	 });

    }    
}

Output :

Input String     : abc=cde,dfe=lk,f,sss=f,d,s

Final properties : 
key = dfe :: value = lk,f
key = sss :: value = f,d,s
key = abc :: value = cde

答案3

得分: 0

public class Test {
    
    public static void main(String args[]) {
        String text = "abc=cde,dfe=lk,f,sss=f,d,s";
        String[] parts = text.split(",");
        Map<String, String> map = new LinkedHashMap<>();
        String key = null;
        StringBuilder value = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].contains("=")) {
                if (key != null) {
                    map.put(key, value.toString());
                    value.setLength(0);
                }
                String[] innerParts = parts[i].split("=");
                key = innerParts[0];
                value.append(innerParts[1]);
            } else {
                value.append(',').append(parts[i]);
            }
        }
        map.put(key, value.toString());
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry);
        }   
    }    
}

Output:

abc=cde

dfe=lk,f

sss=f,d,s

英文:

Full Code :

public class Test {
    
    public static void main(String args[]) {
        String text = &quot;abc=cde,dfe=lk,f,sss=f,d,s&quot;;
        String[] parts = text.split(&quot;,&quot;);
        Map&lt;String, String&gt; map = new LinkedHashMap&lt;&gt;();
        String key = null;
        StringBuilder value = new StringBuilder();
        for (int i = 0; i &lt; parts.length; i++) {
            if (parts[i].contains(&quot;=&quot;)) {
                if (key != null) {
                    map.put(key, value.toString());
                    value.setLength(0);
                }
                String[] innerParts = parts[i].split(&quot;=&quot;);
                key = innerParts[0];
                value.append(innerParts[1]);
            } else {
                value.append(&#39;,&#39;).append(parts[i]);
            }
        }
        map.put(key, value.toString());
        for (Map.Entry&lt;String, String&gt; entry : map.entrySet()) {
            System.out.println(entry);
        }   
    }    
}

Output :

abc=cde

dfe=lk,f

sss=f,d,s

huangapple
  • 本文由 发表于 2020年9月1日 22:12:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63689437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定