Two ways of string segmentation

xiaoxiao2021-04-03  213

Method 1: Use string Split, verify code as follows: import java.util.Arays; public class testsplit {public static void main (string [] args) {string orignstring = new string ("5, 8, 7, 4, 3 , 9, 1 "); string [] Teststring = Orignstring.split (", "); int [] test = {0, 0, 0, 0, 0, 0, 0}; // string to int for (int for) i = 0; i = 0; I -) {system.out.println (test [i]);}}} method 2: use StringTokenizerImport java.util.arrays; import java.util.StringTokenizer; public class splitstringtest {public static void main (String [] args) {string s = new string ("5, 8, 7, 4, 3, 9, 1"); intleth = s.length (); // split s with "," StringTokenizer Commatoker = New StringTokenizer (s, ","); string [] result = new string [commatoker.countToKens ()]; int K = 0; while (Commatoker.h) AsmoreToKens ()) {result [k] = commatoker.nextToken (); k ;} int [] a = new int [result.length]; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-131586.html

New Post(0)