zoukankan      html  css  js  c++  java
  • java解析字符串拆分单独元素

    有时候,需求要求传递多个字符串参数,但是方法参数已经固定为单个String,笔者在学习unity和android之间的消息传递时就遇到这个问题,所以就写了这么一个解析字符串拆分单独元素的方法。

    示例:“@my@red@apple”

    解析为:

    my

    red

    apple

     1 package cutstring;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 import java.util.Scanner;
     6 /**
     7  * 
     8 * @ClassName: MainClass 
     9 * @Description: 字符串格式"@a@b@c",a,b,c会被解析为单独的元素
    10 * @author luxishi
    11 * @date 2016年3月8日 下午5:57:56 
    12 *
    13  */
    14 public class MainClass {
    15 
    16     public static void main(String[] args) {
    17         String m_sentence="@my@red@apple";
    18         List<String> m_list=cutstring(m_sentence);
    19         System.out.println(m_list.size());
    20          for(String tmp:m_list){
    21           System.out.println(tmp);
    22          }
    23 
    24     }
    25  
    26     static List<String> cutstring(String Stence)
    27     {
    28         List<String> stringlist=new ArrayList<String>();//用来存储解析出来的元素
    29         for(int i=0;i<Stence.length();i++)
    30         {
    31             if(Stence.charAt(i)=='@')
    32             { 
    33                 String temp="";//存储单词
    34                 int wordlength=i;
    35                 while(wordlength<Stence.length()-1&&Stence.charAt(++wordlength)!='@')
    36                 {
    37                     temp+=Stence.charAt(wordlength);
    38                     //System.out.println(temp);
    39                 }
    40                 stringlist.add(temp);
    41             }
    42         }
    43         return stringlist;    
    44     }
    45 }
  • 相关阅读:
    HDU 2236 无题II
    P2220 [HAOI2012]容易题
    UVA11383 Golden Tiger Claw
    AT2272 [ARC066B] Xor Sum
    CentOS7 静默安装oracle12c
    SNAT与DNAT
    Linux下离线安装Docker
    TJOI2017 DNA 和 BJOI2015 隐身术
    LOJ6169 相似序列
    BJOI2019 删数
  • 原文地址:https://www.cnblogs.com/luxishi/p/5255041.html
Copyright © 2011-2022 走看看