2013-03-01 44 views
3

我想從字符串得到最後一個子字符串,因爲我有一個文件名tulips.jpg我怎樣才能得到只有.jpg使用子字符串方法?如何獲取Android中的子串?

我用:

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 [email protected] 
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 [email protected] 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 
+0

嗯,是的,它只是採取從字符索引1的子字符串,因爲這是明確你告訴它......「的最後一個子」沒有更多的情況下是沒有意義的。我已經發布了一個完全基於你的例子的答案,但你的問題很不明確。 – 2013-03-01 07:41:11

+0

如果我的問題不清楚,我很抱歉,但感謝您的回答,我正試圖弄清楚。謝謝 – Shweta 2013-03-01 07:46:36

+0

問題是我有不同類型的圖像,如.jpg,.png,.tif,我想如果文件是name.jpg,那麼如果subString是「.jpg」,那麼特定圖像將顯示在我的自定義圖像適配器中。 – Shweta 2013-03-01 09:56:15

回答

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

我們可以通過。然後可以得到兩個值的文件名和擴展名... – Shweta 2013-03-07 07:21:45

4

這聽起來像你想要的一切從第納斯l「。」開始,這將是這樣的:

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

如果淵有「tulips.jgp」的東西,如:

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

將返回字符串withn第一項「鬱金香」的陣列和第二「 JPG」

+0

但是如果我有不同的擴展名,如圖像是.jpg/.png/.pdf,那麼只有該特定的.jpg/.png/.pdf圖像顯示在自定義圖像適配器中。我會進一步的程序,但如何滿足條件。 if(subString ==「.png」){} /不顯示任何結果。 – Shweta 2013-03-01 09:50:30

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()); 
+0

我有不同的文件,如.png,.jpg,.doc,.pdf分機。所以我想如果文件是.png那麼只有.png圖像顯示我在自定義列表適配器中。如果split有.jpg值,則jpg圖像顯示在列表適配器中。所以請建議。 – Shweta 2013-03-01 09:47:04

+0

我想使用String [] str = KEY_SUBFOLDERNAME.split(「。」); \t \t \t \t str [2] =「jpg」; \t \t \t \t \t \t \t \t的System.out.println(STR [1]); \t \t \t \t \t \t \t \t如果(STR [2] == 「JPG」){ \t \t \t \t \t FolderList項=新FolderList(JPGimages [0],Subfolderslist [2],Subfolderslist [1]) ; \t \t \t \t \t Subfolderdata.add(item); – Shweta 2013-03-01 10:03:33

+0

我編輯了我的問題,請參閱 – Shweta 2013-03-01 10:12:16

1

您可以使用此代碼來得到你的答案 這樣的:String Main_String = dsfdsf.jpg

Main_String.substring(path.lastIndexOf("/") + 1); //for`jpg` 

Main_String.substring(path.lastIndexOf("/"));//for .JPG 享受您的代碼:)

1

您可以使用此:

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

In這mainString是一個超級字符串。像「I_AmANDROID.Devloper」和lastString是一個類似「。」的字符串。和startString就像「_」。所以這個函數返回「AmANDROID」。

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" 
0

這需要你的最後四個字符。這是我如何做到這一點:

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