简体   繁体   中英

How to trim a string and store residue in a array?

I'm stuck. (got a thinking barrier right now) :/

I need a stringarray from a string which contaions a lot of "sometext\\n\\t\\t\\t\\t00:00\\n\\t\\t\\t\\t05:32\\n\\t\\t\\t\\t...."
There are always 8 values in this string. I want each (of these 8 ) values in the array[8].
But most importantly are the value. (the text at the beginning is unnecessary).

Would this work:

 var source = "sometext\n\t\t\t\t00:00\n\t\t\t\t05:32\n\t\t\t\t...."
 var result = source.Split(new []{"\n\t\t\t\t"}, StringSplitOptions.None);

that is: guessing that all your values are separated by that newline+4 tabs.

If that is not (always) the separator, then you need to specify how to identify a "value" from a "separator".

I think the following code will do what you need

 int i,j;
 int[] array=new int[8];
 string s="sometext\n\t\t\t\t00:00\n\t\t\t\t05:32\n\t\t\t\t...."; // or input something
 for(i=j=0;i<s.Length;i++){
    if (s[i]>='0'&&s[i]<='9'){
       array[j++]=s[i]-'0';
    }
 }    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM