如何在Android中获取子字符串?

时间:2013-03-01 07:39:40

标签: android string file substring

我正在尝试从String中获取最后一个subString,因为我有一个文件名tulips.jpg如何才能使用substring方法获得.jpg?

我用过:

String Main_String =  dsfdsf.jpg or dfdfgfhfghgh.jpg or dsfsdfgdgsdg.jpg; 

(can have any length, not fixed length)

String str_jpg = Main_String.substring(1, Main_String.length());

/////////////////////

for(int i=0; i < folderResponse.getPropertyCount(); i++) 
        {
            SoapObject SingleFolder = (SoapObject)folderResponse.getProperty(i);

        ID = Integer.parseInt(SingleFolder.getProperty(0).toString());
        KEY_SUBFOLDERNAME = SingleFolder.getProperty(1).toString();
        KEY_SUBJECTTYPE = SingleFolder.getProperty(3).toString();

            String[] str = KEY_SUBFOLDERNAME.split(".");
            for(int j = 0; j < str.length; j++){
                  System.out.println(""+str[j]);
                  // you got all name here use what ever you want.
                  if(str[1] == "jpg"){
                        FolderList item = new FolderList(JPGimages[0], SingleFolder.getProperty(1).toString();, SingleFolder.getProperty(3).toString();;
                        Subfolderdata.add(item);
                    }  

请立即建议

由于

     03-01 16:28:17.115: I/FolderID(3254): 13002
03-01 16:28:17.115: I/User_ID(3254): 1
03-01 16:28:17.175: D/dalvikvm(3254): GC_FOR_ALLOC freed 16K, 5% free 7881K/8263K, paused 52ms
03-01 16:28:17.175: I/dalvikvm-heap(3254): Grow heap (frag case) to 8.042MB for 262160-byte allocation
03-01 16:28:17.255: D/dalvikvm(3254): GC_FOR_ALLOC freed 3K, 6% free 8134K/8583K, paused 70ms
03-01 16:28:17.705: I/AllFolders(3254): anyType{FileFolderDetails=anyType{ID=13003; Name=Doc file; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13004; Name=My Pics; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13005; Name=Testing; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13075; Name=satnam; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13080; Name=Saini; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13084; Name=Jelly; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13092; Name=pushie; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13096; Name=Jarnail_Testing; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13099; Name=amarjeet; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13101; Name=Kuta; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13103; Name=tajmehal; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13105; Name=TextArea; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13107; Name=TextArea1; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13109; Name=TextArea2; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13115; Name=TextArea3; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13121; Name=TextArea5; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13124; Name=Check Char; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13128; Name=amitestinggggg; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13132; Name=kkk; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13136; Name=hhhhhhhhhhhhhhhhhh; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13140; Name=ravitevffvvgfdcvgbgvf; URL=anyType{}; SubjectType=Folder; }; FileFolderDetails=anyType{ID=13002; Name=Capture.PNG; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Capture1.PNG; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Chrysanthemum.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Content4Work.pdf; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Desert.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=DMS_Drag_and_Drop(2).doc; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=groups.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Jellyfish.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Koala.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Lighthouse.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=office.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Penguins.jpg; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Rohit.docx; URL=Tulips.jpg; SubjectType=File; }; FileFolderDetails=anyType{ID=13002; Name=Tulips.jpg; URL=Tulips.jpg; SubjectType=File; }; }
03-01 16:28:17.725: W/System.err(3254): java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
03-01 16:28:17.725: W/System.err(3254):     at com.web.dms.Files_Folders_Activity.subfolderTreedata(Files_Folders_Activity.java:106)
03-01 16:28:17.725: W/System.err(3254):     at com.web.dms.Files_Folders_Activity.onCreate(Files_Folders_Activity.java:56)
03-01 16:28:17.735: W/System.err(3254):     at android.app.Activity.performCreate(Activity.java:4465)
03-01 16:28:17.735: W/System.err(3254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-01 16:28:17.735: W/System.err(3254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-01 16:28:17.735: W/System.err(3254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-01 16:28:17.735: W/System.err(3254):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-01 16:28:17.735: W/System.err(3254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-01 16:28:17.735: W/System.err(3254):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 16:28:17.735: W/System.err(3254):     at android.os.Looper.loop(Looper.java:137)
03-01 16:28:17.745: W/System.err(3254):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-01 16:28:17.745: W/System.err(3254):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 16:28:17.745: W/System.err(3254):     at java.lang.reflect.Method.invoke(Method.java:511)
03-01 16:28:17.745: W/System.err(3254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-01 16:28:17.755: W/System.err(3254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-01 16:28:17.779: W/System.err(3254):     at dalvik.system.NativeStart.main(Native Method)
03-01 16:30:37.056: D/AndroidRuntime(3283): Shutting down VM
03-01 16:30:37.066: W/dalvikvm(3283): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-01 16:30:37.077: E/AndroidRuntime(3283): FATAL EXCEPTION: main
03-01 16:30:37.077: E/AndroidRuntime(3283): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3909)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.ActivityThread.access$1300(ActivityThread.java:122)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1184)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.os.Looper.loop(Looper.java:137)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at java.lang.reflect.Method.invoke(Method.java:511)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at dalvik.system.NativeStart.main(Native Method)
03-01 16:30:37.077: E/AndroidRuntime(3283): Caused by: java.lang.NullPointerException
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
03-01 16:30:37.077: E/AndroidRuntime(3283):     at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
03-01 16:30:37.077: E/AndroidRuntime(3283):     ... 11 more
03-01 16:30:46.506: D/dalvikvm(3315): GC_FOR_ALLOC freed 58K, 4% free 6909K/7171K, paused 52ms
03-01 16:30:46.517: I/dalvikvm-heap(3315): Grow heap (frag case) to 7.309MB for 488976-byte allocation
03-01 16:30:46.656: D/dalvikvm(3315): GC_CONCURRENT freed 1K, 4% free 7385K/7687K, paused 4ms+3ms
03-01 16:30:47.206: D/gralloc_goldfish(3315): Emulator without GPU emulation detected.
03-01 16:30:47.356: W/TextLayoutCache(3315): computeValuesWithHarfbuzz -- need to force to single run
03-01 16:30:52.366: D/InputEventConsistencyVerifier(3315): KeyEvent: ACTION_UP but key was not down.
03-01 16:30:52.366: D/InputEventConsistencyVerifier(3315):   in android.widget.EditText@41051d18
03-01 16:30:52.366: D/InputEventConsistencyVerifier(3315):   0: sent at 20208706000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=20208706, downTime=20208634, deviceId=0, source=0x101 }
03-01 16:30:53.956: I/LoginDetail(3315): Username jarnail@sassypuma.comPassword admin
03-01 16:30:54.036: D/dalvikvm(3315): GC_FOR_ALLOC freed 72K, 3% free 7633K/7815K, paused 44ms
03-01 16:30:54.356: I/myLogin(3315): 1
03-01 16:30:54.726: D/dalvikvm(3315): GC_CONCURRENT freed 362K, 7% free 7694K/8199K, paused 29ms+6ms
03-01 16:30:55.026: I/AllFolders(3315): anyType{TreeData=anyType{ID=13002; FolderName=My Drive; ParentID=0; CreatedBy=1; }; TreeData=anyType{ID=13006; FolderName=Testing1; ParentID=0; CreatedBy=1; }; TreeData=anyType{ID=13007; FolderName=Testing123; ParentID=0; CreatedBy=1; }; }
03-01 16:30:55.506: D/dalvikvm(3315): GC_CONCURRENT freed 362K, 7% free 7767K/8263K, paused 24ms+7ms
03-01 16:30:55.886: W/IInputConnectionWrapper(3315): finishComposingText on inactive InputConnection
03-01 16:30:57.246: I/FolderID(3315): 13002
03-01 16:30:57.246: I/User_ID(3315): 1
03-01 16:30:57.297: D/dalvikvm(3315): GC_FOR_ALLOC freed 17K, 5% free 7880K/8263K, paused 48ms
03-01 16:30:57.297: I/dalvikvm-heap(3315): Grow heap (frag case) to 8.041MB for 262160-byte allocation
03-01 16:30:57.356: D/dalvikvm(3315): GC_FOR_ALLOC freed 3K, 6% free 8133K/8583K, paused 52ms

7 个答案:

答案 0 :(得分:6)

假设您有以下字符串

String Main_String =  "dsfdsf.jpg&&dfdfgfhfghgh.jpg&&dsfsdfgdgsdg.jpg"; 
String[] a = Main_String.split("&&");
for(int i = 0; i < a.length; i++){
  System.out.println(""+a[i]);
  // you got all name here use what ever you want.
}

只想要姓氏:

int last = Main_String.lastIndexOf('&&');
String laatname = Main_String.substring(last-2,Main_String.length());

答案 1 :(得分:5)

听起来你想要最终的所有内容。“以后,这将是:

int lastDot = text.lastIndexOf('.');
if (lastDot == -1) {
    // No dots - what do you want to do?
} else {
    String extension = text.substring(lastDot);
}

答案 2 :(得分:2)

你可以用这个:

public static String getSubString(String mainString, String lastString, String startString) {
    String endString = "";
    int endIndex = mainString.indexOf(lastString);
    int startIndex = mainString.indexOf(startString);
    Log.d("message", "" + mainString.substring(startIndex, endIndex));
    endString = mainString.substring(startIndex, endIndex);
    return endString;
}

在此mainString是一个超级字符串。喜欢&#34; I_AmANDROID.Devloper&#34; lastString是一个类似&#34;的字符串。&#34;和startString就像&#34; _&#34;。所以这个函数返回&#34; AmANDROID&#34;。

答案 3 :(得分:1)

如果你的“tulips.jpg”有类似的东西:

"tulips.jpg".split("\\.")

将返回一个字符串数组,第一项为“郁金香”,第二项为“jpg”

答案 4 :(得分:0)

从所有人那里得到答案后......我想我已经使用了StringTokenizer。它真的很好用。

       StringTokenizer tokens = new StringTokenizer(KEY_SUBFOLDERNAME, ".");
                String first_string = tokens.nextToken();
                File_Ext = tokens.nextToken();
                System.out.println("First_string : "+first_string);
                System.out.println("File_Ext : "+File_Ext);

答案 5 :(得分:0)

作为Jeen Broekstra,

如果这样的输入会发生什么?

String Input = " ,, Num1: 12, Num2: 23, ";
String n1 = getSubString(Input, ",", "Num1:");

所以我认为稍微改变一下:

public static String getSubString(String mainString, String startString, String lastString) {
    String endString = "";

    int startIndex = mainString.indexOf(startString);

    startIndex += startString.length();

    int endIndex = mainString.indexOf(lastString,startIndex );

    endString = mainString.substring(startIndex, endIndex);

    return endString;
}

String Input =" ,, Num1: 12, Num2: 23, ";
String n1=getSubString(Input,"Num1:",","); // n1=" 12"
String n2=getSubString(Input,"Num:",","); // n2=" 23"

答案 6 :(得分:0)

这需要你获得最后四个字符。我是这样做的:

String fileName = "tulips.jpg";
String fileExt = fileName.substring(fileName.length() - 4);
Toast.makeText(this, "File extension is: "+ fileExt, Toast.LENGTH_LONG).show();