Sometimes the requirements require multiple string parameters to be passed, but the method parameters are fixed to a single string, and I encounter this problem when I learn the message passing between unity and Android, so I write a method of splitting individual elements into a parsing string.
Example: "@[email protected] @apple"
resolves to:
My
Red
Apple
1 Packagecutstring;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 ImportJava.util.Scanner;6 /**7 * 8 * @ClassName: MainClass9 * @Description: string format "@[email protected]@c", A,b,c will be parsed as a separate elementTen * @authorLuxishi One * @date March 8, 2016 PM 5:57:56 A * - */ - Public classMainClass { the - Public Static voidMain (string[] args) { -String m_sentence= "@[email protected] @apple"; -List<string> m_list=cutstring (m_sentence); + System.out.println (M_list.size ()); - for(String tmp:m_list) { + SYSTEM.OUT.PRINTLN (TMP); A } at - } - - StaticList<string>cutstring (String stence) - { -List<string> stringlist=NewArraylist<string> ();//used to store parsed elements . in for(intI=0;i<stence.length (); i++) - { to if(Stence.charat (i) = = ' @ ') + { -String temp= "";//Store Words the intWordlength=i; * while(Wordlength<stence.length () -1&&stence.charat (++wordlength)! = ' @ ') $ {Panax Notoginsengtemp+=Stence.charat (wordlength); - //System.out.println (temp); the } + Stringlist.add (temp); A } the } + returnstringlist; - } $}
Java parsing string splitting individual elements