You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Objective-C中如何从非格式化字符串提取测量值并存入数组?

Hey there! Let's figure out how to pull those measurement values from your Kane device's raw string. Since you can already print the unformatted output, the main task here is parsing that text to extract exactly what you need—serial number, log ID, date, time, CO₂, CO, and more.

Step 1: Use Regular Expressions for Labeled Raw Strings

Most Kane devices output data with clear labels (like "Serial Number:", "CO₂:"), so regular expressions are perfect for targeting these fields. Here's a practical example using JavaScript (adjust to your language if needed):

// Replace this with your actual raw string from the device
const rawDeviceData = "Serial Number: K98765 | Log ID: 0042 | Date: 2024-05-21 | Time: 09:15:30 | CO₂: 480 ppm | CO: 0.015 %";

// Define regex patterns for each value you want to extract
const fieldPatterns = {
  serialNumber: /Serial Number: (\w+)/,
  logId: /Log ID: (\d+)/,
  date: /Date: (\d{4}-\d{2}-\d{2})/,
  time: /Time: (\d{2}:\d{2}:\d{2})/,
  co2: /CO₂: (\d+) ppm/,
  co: /CO: ([\d.]+) %/
};

// Extract values and store in an array (either objects with labels or raw values)
const extractedMeasurements = [];
for (const [fieldName, pattern] of Object.entries(fieldPatterns)) {
  const match = rawDeviceData.match(pattern);
  if (match) {
    // If you want an array of labeled objects:
    extractedMeasurements.push({ [fieldName]: match[1] });
    // If you want a plain array of values only:
    // extractedMeasurements.push(match[1]);
  } else {
    // Handle cases where a field might be missing
    extractedMeasurements.push({ [fieldName]: "Not found" });
  }
}

console.log(extractedMeasurements);

Step 2: Handle Unlabeled/Delimited Strings

If your device returns data without labels (like comma-separated values: K98765,0042,2024-05-21,09:15:30,480,0.015), you can split the string directly and map values to their names:

const rawDeviceData = "K98765,0042,2024-05-21,09:15:30,480,0.015";
const fieldNames = ["serialNumber", "logId", "date", "time", "co2", "co"];

const extractedMeasurements = rawDeviceData.split(',')
  .map((value, index) => ({ [fieldNames[index]]: value.trim() }));

console.log(extractedMeasurements);

Step 3: Clean and Validate Extracted Data

Don't forget to convert numeric values (like CO₂ and CO) to proper number types and add basic validation:

const cleanedMeasurements = extractedMeasurements.map(item => {
  if (item.co2) item.co2 = parseInt(item.co2, 10);
  if (item.co) item.co = parseFloat(item.co);
  // Add date validation if needed
  if (item.date && !/^\d{4}-\d{2}-\d{2}$/.test(item.date)) {
    item.date = "Invalid date format";
  }
  return item;
});

If your device's raw string has a unique format (like different separators or label wording), just tweak the regex patterns or split logic to match it.

内容的提问来源于stack exchange,提问作者Abhinaba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:16:33