androidsubstring
Ⅰ android 几个经常用到的字符串的截取
几个经常用到的字符串的截取
string str="123abc456";
int i=3;
1 取字符串的前i个字符
   str=str.Substring(0,i); // or  str=str.Remove(i,str.Length-i); 
2 去掉字符串的前i个字符:
   str=str.Remove(0,i);  // or str=str.Substring(i); 
3 从右边开始取i个字符:
  str=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i);
4 从右边开始去掉i个字符:
   str=str.Substring(0,str.Length-i); // or str=str.Remove(str.Length-i,i);
5 判断字符串中是否有"abc" 有则去掉之
   using System.Text.RegularExpressions;
   string str = "123abc456";
   string a="abc";
   Regex r = new  Regex(a); 
   Match m = r.Match(str); 
   if (m.Success)
   {
    //绿色部分与紫色部分取一种即可。
      str=str.Replace(a,"");
      Response.Write(str);   
      string str1,str2;
      str1=str.Substring(0,m.Index);
      str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);
      Response.Write(str1+str2); 
   }
6 如果字符串中有"abc"则替换成"ABC"
   str=str.Replace("abc","ABC");
************************************************
string str="adcdef"; int indexStart = str.IndexOf("d");
int endIndex =str.IndexOf("e");
string toStr = str.SubString(indexStart,endIndex-indexStart);
c#截取字符串最后一个字符的问题!
str1.Substring(str1.LastIndexOf(",")+1)
Ⅱ android 中如何限制 EditText 最大输入字符数
方法一:
在 xml 文件中设置文本编辑框属性作字符数限制
如:android:maxLength="10" 即限制最大输入字符个数为10
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
[java] view plainprint?
01.public class TextEditActivity extends Activity {
02.    /** Called when the activity is first created. */
03.    @Override
04.    public void onCreate(Bundle savedInstanceState) {
05.        super.onCreate(savedInstanceState);
06.        setContentView(R.layout.main);
07.
08.        EditText editText = (EditText)findViewById(R.id.entry);
09.        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
10.    }
11.}
public class TextEditActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText)findViewById(R.id.entry);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
}
}
方法三:
利用 TextWatcher 进行监听
[java] view plainprint?
01.package cie.textEdit;
02.
03.import android.text.Editable;
04.import android.text.Selection;
05.import android.text.TextWatcher;
06.import android.widget.EditText;
07.
08./*
09. * 监听输入内容是否超出最大长度,并设置光标位置
10. * */
11.public class MaxLengthWatcher implements TextWatcher {
12.
13.    private int maxLen = 0;
14.    private EditText editText = null;
15.
16.
17.    public MaxLengthWatcher(int maxLen, EditText editText) {
18.        this.maxLen = maxLen;
19.        this.editText = editText;
20.    }
21.
22.    public void afterTextChanged(Editable arg0) {
23.        // TODO Auto-generated method stub
24.
25.    }
26.
27.    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
28.            int arg3) {
29.        // TODO Auto-generated method stub
30.
31.    }
32.
33.    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
34.        // TODO Auto-generated method stub
35.        Editable editable = editText.getText();
36.        int len = editable.length();
37.
38.        if(len > maxLen)
39.        {
40.            int selEndIndex = Selection.getSelectionEnd(editable);
41.            String str = editable.toString();
42.            //截取新字符串
43.            String newStr = str.substring(0,maxLen);
44.            editText.setText(newStr);
45.            editable = editText.getText();
46.
47.            //新字符串的长度
48.            int newLen = editable.length();
49.            //旧光标位置超过字符串长度
50.            if(selEndIndex > newLen)
51.            {
52.                selEndIndex = editable.length();
53.            }
54.            //设置新光标所在的位置
55.            Selection.setSelection(editable, selEndIndex);
56.
57.        }
58.    }
59.
60.}
package cie.textEdit;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;
/*
* 监听输入内容是否超出最大长度,并设置光标位置
* */
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
	
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
		
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
		
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Editable editable = editText.getText();
int len = editable.length();
		
if(len > maxLen)
{
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字符串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
			
//新字符串的长度
int newLen = editable.length();
//旧光标位置超过字符串长度
if(selEndIndex > newLen)
{
selEndIndex = editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable, selEndIndex);
			
}
}
}
对应的 activity 部分的调用为:
[java] view plainprint?
01.package cie.textEdit;
02.
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.text.InputFilter;
06.import android.widget.EditText;
07.
08.public class TextEditActivity extends Activity {
09.    /** Called when the activity is first created. */
10.    @Override
11.    public void onCreate(Bundle savedInstanceState) {
12.        super.onCreate(savedInstanceState);
13.        setContentView(R.layout.main);
14.
15.        EditText editText = (EditText) findViewById(R.id.entry);
16.        editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
17.
18.    }
19.}
package cie.textEdit;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.widget.EditText;
public class TextEditActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText) findViewById(R.id.entry);
editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
}
}
限制输入字符数为10个
main.xml 文件
[html] view plainprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.    android:layout_width="fill_parent"
04.    android:layout_height="fill_parent">
05.    <TextView
06.        android:id="@+id/label"
07.        android:layout_width="fill_parent"
08.        android:layout_height="wrap_content"
09.        android:text="Type here:"/>
10.    <EditText
11.        android:id="@+id/entry"
12.        android:singleLine="true"
13.        android:layout_width="fill_parent"
14.        android:layout_height="wrap_content"
15.        android:background="@android:drawable/editbox_background"
16.        android:layout_below="@id/label"/>
17.    <Button
18.        android:id="@+id/ok"
19.        android:layout_width="wrap_content"
20.        android:layout_height="wrap_content"
21.        android:layout_below="@id/entry"
22.        android:layout_alignParentRight="true"
23.        android:layout_marginLeft="10dip"
24.        android:text="OK" />
25.    <Button
26.        android:layout_width="wrap_content"
27.        android:layout_height="wrap_content"
28.        android:layout_toLeftOf="@id/ok"
29.        android:layout_alignTop="@id/ok"
30.        android:text="Cancel" />
31.</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
Ⅲ 安卓字符串处理,比如我有个长度为10的字符串,我想要截取掉前5个,只剩后面5个,该用什么方法
.substring(start, end);
Ⅳ android stdio equals怎么匹配文字
这样使用:
String str=“test”;String str2=“test2”;String str3=str+str2; 字符串的操作与Java一样,支持拼接,截取(substring方法),比对(equals方法)等等。不知道您对字符串操作有什么需求,或者有什么疑问呢,您可以继续追问。
在if的判断括号里加上一个感叹号就行了。 比如: if(a.equals(b))表示判断a等于b是否成立 if(!a.equals(b))表示判断a不等于b是否成立
关于在hibernate的pojo类中,重新equals()和hashcode()的问题: 1),重点是equals,重写hashCode只是技术要求(为了提高效率) 2),为什么要重写equals呢,因为在java的集合框架中,是通过equals来判断两个对象是否相等的 3),在hibernate中,经。
仔细读官方的API: Returns true if the string is null or 0-length. 因为你从EditText返回的是一个变量。如果这个变量本身为null值,那么你掉它的equals方法是要报错的。但是如果你调用TextUtils.isEmpty() 把这个变量作为参数传进去。只要这。
Android studio常用快捷设置方法如下: 1.设置鼠标移到类上面会有说明出现(跟Eclipse对应)。(进入设置界面:File->Settings) 2.快捷键Alt+Enter.(自动导包,还有提示一些错误的解决方法【如需要加try catch什么的,会提示】) 3.快捷键Alt+In。
1、新建test文件夹在要测试工程目录的兄弟目录,新建一个test文件夹,
2、测试代码我在这个test文件夹中添加了一个类EexampleTest,该类派生自InstrumentationTestCase,并写了一个方法,完整代码如下: [java]view plain publicclass。
测试用例是什么,测试用例其实就是一段普通的程序代码,通常是带有期望的运行结果的,测试者可以根据最终的运行结果来判断程序是否能正常工作。 单元测试是什么,单元测试是指对软件中最小的功能模块进行测试,如果软件的没一个单元都能通过测试。
Ⅳ 如何获取android 进程信息
1.获取系统的可用内存和总内存。
获取系统内存中应用的信息,需要用到ActivityManager这个类,然而当你用这个类拿数据的时候你会发现,拿到的数据不正确。用这个类的API获取系统的总内存和可用内存会出现数据不正确的情况。除了这个类,Android手机中有文件描述了这些信息——/proc/meminfo。meminfo文件中详细的记录了安卓手机的一些数据,包括可用内存和总内存。附上代码:
 public static long getTotalMemSize() {
        long size=0;
        File file = new File("/proc/meminfo");
        try {
            BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String memInfo = buffer.readLine();
            int startIndex = memInfo.indexOf(":");
            int endIndex = memInfo.indexOf("k");
            memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
            size = Long.parseLong(memInfo);
            size *= 1024;
            buffer.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        return size;
    }
    public static long getAviableMemSize() {
        long size=0;
        File file = new File("/proc/meminfo");
        try {
            BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
             String memInfos=new String();
            int i=0;
            while ((memInfos=buffer.readLine())!=null){
               i++;
                if (i==2){
                    memInfo = memInfos;
                }
            }
            int startIndex = memInfo.indexOf(":");
            int endIndex = memInfo.indexOf("k");
            memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
            size = Long.parseLong(memInfo);
            size *= 1024;
            buffer.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        return size;
    }
操作很简单分别是读取第一行的数据和第二行的数据,将字符串分去出,将所得值乘以1024变为byte类型。
2.获取内存中运行应用的信息
首先,自然要有一个Bean文件用于存储这些信息,之后通过ActivityManager的getRunningAppProcesses()方法得到一个RunningAppProcessInfo的List。便利这个List去除我们想要的数据,存在我们的Bean文件夹中。
  public static List<TaskBean> getAllTask() {
        List<TaskBean>taskList=new ArrayList<>();
        List<ActivityManager.RunningAppProcessInfo>runList=UIUtils.getActManager().getRunningAppProcesses();
        try {
            for (ActivityManager.RunningAppProcessInfo r:runList) {
                TaskBean taskBean = new TaskBean();
                String processName = r.processName;
                taskBean.setPackageName(processName);
                PackageInfo packageInfo = UIUtils.getPacManager().getPackageInfo(processName, 0);
                taskBean.setIcon(packageInfo.applicationInfo.loadIcon(UIUtils.getPacManager()));
                taskBean.setName(packageInfo.applicationInfo.loadLabel(UIUtils.getPacManager()).toString());
                Debug.MemoryInfo[] processInfo=UIUtils.getActManager().getProcessMemoryInfo(new int[]{r.pid});
                taskBean.setMemSize(processInfo[0].getTotalPrivateDirty()*1024);
                if ((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0){
                    taskBean.setSystem(true);
                }else {
                    taskBean.setUser(true);
                }
                if (taskList != null) {
                    taskList.add(taskBean);
                    for (int i=0;i<taskList.size();i++) {
                        if (taskList.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){
                            taskList.remove(i);
                        }
                    }
                }
            }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        return taskList;
    }
好了,大功告成。当你开开心心的拿到手机上调试的时候你会发现,一个数据都没有。原来,在Android5.0之后,谷歌处于完全考虑已经弃用了通过如上方法拿到进程中的信息。那么又应该怎么做呢?
 public static List<TaskBean> getTaskInfos() {
        List<AndroidAppProcess> processInfos = ProcessManager.getRunningAppProcesses();
        List<TaskBean> taskinfos = new ArrayList<TaskBean>();
        // 遍历运行的程序,并且获取其中的信息
        for (AndroidAppProcess processInfo : processInfos) {
            TaskBean taskinfo = new TaskBean();
            // 应用程序的包名
            String packname = processInfo.name;
            taskinfo.setPackageName(packname);
            // 湖区应用程序的内存 信息
            android.os.Debug.MemoryInfo[] memoryInfos = UIUtils.getActManager()
                    .getProcessMemoryInfo(new int[] { processInfo.pid });
            long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024L;
            taskinfo.setMemSize(memsize);
            taskinfo.setPackageName(processInfo.getPackageName());
            try {
                // 获取应用程序信息
                ApplicationInfo applicationInfo = UIUtils.getPacManager().getApplicationInfo(
                        packname, 0);
                Drawable icon = applicationInfo.loadIcon(UIUtils.getPacManager());
                taskinfo.setIcon(icon);
                String name = applicationInfo.loadLabel(UIUtils.getPacManager()).toString();
                taskinfo.setName(name);
                if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                    // 用户进程
                    taskinfo.setUser(true);
                } else {
                    // 系统进程
                    taskinfo.setSystem(true);
                }
            } catch (PackageManager.NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                // 系统内核进程 没有名称
                taskinfo.setName(packname);
                Drawable icon = UIUtils.getContext().getResources().getDrawable(
                        R.drawable.ic_launcher);
                taskinfo.setIcon(icon);
            }
            if (taskinfo != null) {
                taskinfos.add(taskinfo);
                for (int i=0;i<taskinfos.size();i++) {
                    if (taskinfos.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){
                        taskinfos.remove(i);
                    }
                }
            }
        }
        return taskinfos;
    }
好了,接下来只需要判断安装的版本就可以了:
int sysVersion = Integer.parseInt(Build.VERSION.SDK);
taskList = sysVersion > 21 ? TaskManagerEngine.getTaskInfos() : TaskManagerEngine.getAllTask();
好了,大功告成。数据就能正常拿到了。
