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

Android开发:如何将TextView特定格式文本转换为字符串数组?

How to Convert TextView's JSON-like String to a String Array in Android

Hey there! I see you're trying to turn that curly-brace wrapped string from your TextView into a proper String[] for your Android project — let's break this down into simple, actionable steps.

First, Let's Clarify the Input

Your TextView holds content that looks like this:

{"100$","575$","50$","70$","60$"}

This is almost a valid JSON array, except it uses curly braces instead of square brackets. We can handle this with two approaches: quick manual string cleanup, or robust JSON parsing (the more reliable option for long-term use).


Method 1: Manual String Manipulation (Quick & Straightforward)

If you’re confident the TextView content will always follow this exact format, you can clean and split the string directly:

// Grab the TextView and its raw content
TextView textView = findViewById(R.id.textView);
String rawText = textView.getText().toString().trim();

// Step 1: Strip off the curly braces from the start and end
String noBraces = rawText.substring(1, rawText.length() - 1);

// Step 2: Split the string by commas, ignoring any extra spaces around items
String[] splitItems = noBraces.split(",\\s*");

// Step 3: Remove quotes from each item and trim leftover whitespace
String[] amountArray = new String[splitItems.length];
for (int i = 0; i < splitItems.length; i++) {
    amountArray[i] = splitItems[i].replace("\"", "").trim();
}

This works great for fixed, predictable formats, but it’ll break if the text ever has unexpected extra characters or formatting shifts.


Method 2: JSON Parsing (Robust & Error-Resilient)

For a more stable solution, convert the text to a valid JSON array and use Android’s built-in org.json library to parse it:

import org.json.JSONArray;
import org.json.JSONException;

// ...

TextView textView = findViewById(R.id.textView);
String rawText = textView.getText().toString().trim();

// Fix the format: replace curly braces with square brackets to make valid JSON
String validJson = rawText.replace("{", "[").replace("}", "]");

String[] amountArray;
try {
    // Parse the JSON array
    JSONArray jsonArray = new JSONArray(validJson);
    amountArray = new String[jsonArray.length()];
    
    // Pull each string value from the JSON array
    for (int i = 0; i < jsonArray.length(); i++) {
        amountArray[i] = jsonArray.getString(i);
    }
} catch (JSONException e) {
    // Handle parsing errors (e.g., invalid text format)
    e.printStackTrace();
    amountArray = new String[0]; // Fallback to empty array if parsing fails
}

This method is better for long-term maintenance because it handles edge cases (like extra spaces or minor format tweaks) gracefully, and it’s easier to adjust if your input format changes later.


Quick Pro Tip

If you control how the text gets into the TextView in the first place, store it as a valid JSON array (using square brackets [] instead of curly braces {}) from the start — this will skip the format-fixing step and make parsing even smoother!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:45:58