java 删除前缀的字符串操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15338576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:17:55  来源:igfitidea点击:

Operation on strings to remove prefix

javaandroidstringsubstring

提问by Sharp

final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
final PackageManager pm = getApplicationContext().getPackageManager();
ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);

//Drawable iconApp = resolveInfo.activityInfo.loadIcon(getPackageManager());

ApplicationAdapter adapter = new ApplicationAdapter(this, listP);

adapter.addListener(this);
ListView list = (ListView)findViewById(R.id.list);

list.setAdapter(adapter);

This code displays all applications available but I would like to work on the result. On every line you've got com.android.*and it's that part I would like to cut.

此代码显示所有可用的应用程序,但我想处理结果。在您拥有的每一行上com.android.*,这就是我想剪掉的部分。

The problem is when I tried to use substring(10)for example it doesn't change the result.

问题是当我尝试使用时substring(10),例如它不会改变结果。

i tried that way and with Log i success the substring but when i display it on the screen it just show me another layout with all his buttons i almost get it with this code i display with Log exactly what i want to put on screens but there is just a black board while i got the right result with the Log i can't see the reason

我尝试过这种方式,并且使用 Log 我成功了子字符串,但是当我在屏幕上显示它时,它只是向我显示了另一个带有他所有按钮的布局只是一个黑板,而我用日志得到了正确的结果我看不到原因

public void onCreatebis() {

        setContentView(R.layout.main);
        final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
        final PackageManager pm = getApplicationContext().getPackageManager();
        ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);
        final int trimLength = "com.android.".length();
        ArrayList<String> maliste = new ArrayList();
        int size=listP.size();
        size=maliste.size();
       //String []maliste=new String[listP.size()];
        // Loop over each item.
        for (ResolveInfo info : listP) {
            // Get the (full, qualified) package name.
            String packag = info.activityInfo.applicationInfo.packageName;

            // Now, trim it with substring and the trim length.
            String trimmed = packag.substring(trimLength);
            for(int i=0;i<maliste.size();i++){
                maliste.set(i, trimmed);
            }
            Log.v("trimmed", trimmed);

            // [ do whatever you want with the trimmed name ]
        }




        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste);

        ListView list = (ListView)findViewById(R.id.list);

        list.setAdapter(adapter2);

采纳答案by wchargin

You have the right idea to use substring. Here's how I would go about doing it:

你有正确的想法来使用substring. 这是我将如何去做:

// Get the length of text to trim.
final int trimLength = "com.android.".length();

// Loop over each item.
for (ResolveInfo info : listP) {
    // Get the (full, qualified) package name.
    String package = info.activityName.packageName;

    // Now, trim it with substring and the trim length.
    String trimmed = package.substring(trimLength);

    // [ do whatever you want with the trimmed name ]
}

The value of trimLengthevaluates to 12, so I'm not sure how you got 10, but this should work.

的值为trimLength12,所以我不确定你是如何得到 10 的,但这应该有效。

回答by Sharp

thank you WChargin with your help i did this there are small differences but i don't know their deep diffrence anyway that code runs well

谢谢 WChargin 与您的帮助

public void onCreatebis() {

        setContentView(R.layout.main);
        final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
        final PackageManager pm = getApplicationContext().getPackageManager();
        ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);
        final int trimLength = "com.android.".length();
        ArrayList<String> maliste = new ArrayList<String>();


       //String []maliste=new String[listP.size()];
        // Loop over each item.
        for (ResolveInfo info : listP) {
            // Get the (full, qualified) package name.
            String packag = info.activityInfo.applicationInfo.packageName;

            // Now, trim it with substring and the trim length.
            String trimmed = packag.substring(trimLength);
            maliste.add(trimmed);



        }

        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste);

        ListView list = (ListView)findViewById(R.id.list);

        list.setAdapter(adapter2);



    }

the problem with that solution is that i've got a string at the end and of course i can manage to create an array of all strings and give it to an adapter but there were other operation i couldn't make anymore because it were using an arraylist of ResolveInfoand not an arraylist of string add your own listener to a listhttps://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener

该解决方案的问题是我最后有一个字符串,当然我可以设法创建一个所有字符串的数组并将其提供给适配器,但是还有其他操作我无法再进行,因为它正在使用ResolveInfo 的数组列表而不是字符串 的数组列表将您自己的侦听器添加到列表中https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener