Android系统。如何将String =(A B,C D,E F,.....)切换为String(B A,D C,F E,......)

时间:2017-05-04 07:02:01

标签: android arrays string

我是初学者。

我从SQLite获得String。

            WKT = cursor.getString(cursor.getColumnIndex(polygon));
            WKT = WKT.replaceAll(",", ",  ");

它看起来像String =(1.00 2.00,3.00 4.00,5.00 6.00,.....)。 如何将其切换为String(2.00 1.00,4.00 3.00,6.00 5.00,......)。

感谢。

5 个答案:

答案 0 :(得分:1)

试试这个

private static void testMethod() {

    String result = "(A B, C D, E F)";
    String resultTmp =  result;

    resultTmp = resultTmp.replace("(", "");
    resultTmp = resultTmp.replace(")", "");

    String[] aryResult = resultTmp.split(",");

    String[] finalResult = reverseString(aryResult);

    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < finalResult.length; i++) {
        String dfgf = strBuilder.toString().equals("") ? "" : ",";
        strBuilder.append(dfgf);
        strBuilder.append(finalResult[i]);
    }
    String newString = strBuilder.toString();
    System.out.println("RESULT : " + newString);

}


public static String[] reverseString(String[] words)
{
    String[] t = new String[words.length];

    for (int i = 0; i < words.length; i++)
    {
        t[i] = "";
        for (int j = words[i].length() - 1; j >= 0; j--)
            t[i] += words[i].charAt(j);
    }
    return t;
}

答案 1 :(得分:1)

Follow below steps

1)Split string with ","
2)User Reverse() function to reverse the string(AB->BA)
3)Store the result in to an array.

Repeat the step until you split the last entry from sql lite.

答案 2 :(得分:0)

尝试这个逻辑,它在c和c ++中工作。

 int Groups = 1; // Count 1 for the first group of letters
for ( int Loop1 = 0; Loop1 < strlen(String); Loop1++)
  if (String[Loop1] == ' ') // Any extra groups are delimited by space
    Groups += 1;

int* GroupPositions = new int[Groups]; // Stores the positions
for ( int Loop2 = 0, Position = 0; Loop2 < strlen(String); Loop2++)
{
  if (String[Loop2] != ' ' && (String[Loop2-1] == ' ' || Loop2-1 < 0))
  {
    GroupPositions[Position] = Loop2; // Store position of the first letter
    Position += 1; // Increment the next position of interest
  }
}

答案 3 :(得分:0)

试试这个..

private static void testTwo() {
    String result = "(1.00 2.00, 3.00 4.00, 5.00 6.00)";
    String resultTmp =  result;

    resultTmp = resultTmp.replace("(", "");
    resultTmp = resultTmp.replace(")", "");

    String[] aryResult = resultTmp.split(",");

    for (int i = 0; i < aryResult.length; i++) {
        String str = aryResult[i].trim();
        String[] sdf = str.split(" ");
        aryResult[i] = reverseArray(sdf);
    }
    String strResult = Arrays.toString(aryResult).replace("[", "(").replace("]", ")");
    System.out.println("RESULT : " + strResult);
}

public static String reverseArray(String[] words)
{
    for(int i = 0; i < words.length / 2; i++)
    {
        String temp = words[i];
        words[i] = words[words.length - i - 1];
        words[words.length - i - 1] = temp;
    }
    return ((Arrays.toString(words)).replace("[", "")).replace("]", "").replace(",", "");
}

答案 4 :(得分:0)

<强>#。如果您想交换sampleIndex[i-1],请使用以下方法:

sub-string

使用:

public String swapString(String str) {

    str = str.replace("(", "");
    str = str.replace(")", "");

    String[] array = str.split(", ");
    StringBuilder result = new StringBuilder();
    result.append("(");

    for (int i = 0; i < array.length; i++) {

        String[] temp =  String.valueOf(array[i]).split(" ");

        result.append(temp[1]);
        result.append(" ");
        result.append(temp[0]);

        if (i != array.length -1)
            result.append(", ");
    }
    result.append(")");

    return result.toString();
}

<强>输出:

String yourString  = "(1.00 2.00, 3.00 4.00, 5.00 6.00)";

Log.d("ARRAY", "Before Swap: " + yourString);

yourString = swapString(yourString);

Log.d("ARRAY", "After Swap: " + yourString);

<强>#。如果您想交换D/ARRAY: Before Swap: (1.00 2.00, 3.00 4.00, 5.00 6.00) D/ARRAY: After Swap: (2.00 1.00, 4.00 3.00, 6.00 5.00) ,请使用以下方法:

char

使用:

public String swapChar(String str) {
    // Convert string to char array
    char[] array = str.toCharArray();

    for (int i = 1; i < array.length - 1; i+=5) {
        // Swap char
        char temp = array[i];
        array[i] = array[i+2];
        array[i+2] = temp;
    }

    return String.valueOf(array);
}

<强>输出:

String yourString = "(A B, C D, E F, G H, I J)";

Log.d("ARRAY", "Before Swap: " + yourString);

yourString = swapChar(yourString);

Log.d("ARRAY", "After Swap: " + yourString);

希望这会有所帮助〜