简体   繁体   English

安卓 如何将String =(AB,CD,EF,…)切换为String(BA,DC,FE,…)

[英]Android. How to switch String = (A B, C D, E F, …) to String (B A, D C, F E, …)

i am a beginner. 我是一个初学者。

I get String from SQLite. 我从SQLite获取字符串。

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

Its look like String = (1.00 2.00, 3.00 4.00, 5.00 6.00, .....). 它看起来像String =(1.00 2.00,3.00 4.00,5.00 6.00,.....)。 How to switch it to String (2.00 1.00, 4.00 3.00, 6.00 5.00, ......). 如何将其切换为字符串(2.00 1.00,4.00 3.00,6.00 5.00,......)。

Thanks. 谢谢。

try this 尝试这个

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;
}
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.

Try this logic,its working in c and c++. 试试这个逻辑,它可以在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
  }
}

try this.. 尝试这个..

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(",", "");
}

#. If you want to swap sub-string , just use below method: 如果要交换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();
}

USE: 采用:

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);

OUTPUT: 输出:

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)

#. If you want to swap char , use below method: 如果要交换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);
}

USE: 采用:

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);

OUTPUT: 输出:

D/ARRAY: Before Swap: (A B, C D, E F, G H, I J)
D/ARRAY: After Swap: (B A, D C, F E, H G, J I)

Hope this will help~ 希望这会有所帮助〜

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android SQLite 查询字符串到达​​ A~B~C~D~E~F 中的 D,其中 A、B、C、D、E 和 F 可以有任何感兴趣的长度和数据 - Android SQLite query string to reach to D in A~B~C~D~E~F where A,B,C,D,E and F could have any interested length and data 仅显示自定义字母 - a、b、c、d、e(Android 键盘) - Show custom alphabets - a,b,c,d,e only (Android Keyboard) 如何完成A,B,C,D,E的特定活动? - How to finish specific activity from A,B,C, D, E? Android,我可以将NumberPicker更改为CharPicker {A +,A,B +,B.C +,C,D +,D,F} - Android , Could I change NumberPicker to CharPicker {A+,A,B+,B.C+,C,D+,D,F} 错误:(1,0)插件太旧而且ANDROID_DAILY_OVERRIDE值也过时,请使用新值:“11f0c8f6a19f6b805f4d0f4ecbec99d777fb7c60” - Error:(1, 0) Plugin is too old and ANDROID_DAILY_OVERRIDE value is also outdated, please use new value :“11f0c8f6a19f6b805f4d0f4ecbec99d777fb7c60” 如何在 android (0x7f0b005e) R.java 中更改这些数字 - How to change these numbers in android (0x7f0b005e) R.java 令牌 android.os.BinderProxy@e4f4f2b 无效; 您的活动正在运行吗? - token android.os.BinderProxy@e4f4f2b is not valid; is your activity running? 找不到ID为0x7f0e00d5(com.example.page:id/dialogViewpager)的片段OneFragment {92c0220#1 id = 0x7f0e00d5 android:switcher:2131624149:0} - No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragment{92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0} sh -f和-d被视为-e - sh -f and -d are being treated as -e 如何恢复碎片? 我在调用这些片段的不同按钮上创建了四个片段-A,B,C,D - How to resume the fragments ? I have created four fragments -A,B,C,D on diffrent buttons im calling these fragments e.g
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM