Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中包装文本视图中的数字_Java_Android_Replace_Textview - Fatal编程技术网

Java 如何在android中包装文本视图中的数字

Java 如何在android中包装文本视图中的数字,java,android,replace,textview,Java,Android,Replace,Textview,在Android中,我有一个TextView,在TextView中,它包含一些文本示例(22个like,有时是1个like或空文本)。在这里,我只想获取数字而不是字符。因此,使用如何使用String.replaceAll()来准确获取数字,而不需要空文本。 下面是一个示例代码 textview.setText("2 Likes"); String com=textview.replaceAll("",""); 使用String.split将数组中的字符串拆分为: textview.setTex

在Android中,我有一个TextView,在TextView中,它包含一些文本示例(22个like,有时是1个like或空文本)。在这里,我只想获取数字而不是字符。因此,使用如何使用String.replaceAll()来准确获取数字,而不需要空文本。 下面是一个示例代码

textview.setText("2 Likes");
String com=textview.replaceAll("","");

使用
String.split
将数组中的字符串拆分为:

textview.setText("2 Likes");
String strtxt=textview.getText();

String[] arrliks = strtxt.split(" ");  
String num_of_likes=arrliks[0]; //<<< get 2 here

如果你的文字总是这样,这意味着(32个喜欢,2个喜欢)

可以使用子字符串函数

If(yourstring.length>0)
{

    string result = yourstring.substring(0,yourstring.index(" "));
}
else
{
Do whatever here..
}

这将导致单独剪切数字部分。但是请确保在数字之后始终有一个空格。

使用正则表达式可以轻松找到喜欢的数字:

     String txt = textview.getText();
     Pattern p = Pattern.compile("^([0-9]+).*");
     Matcher m = p.matcher(txt);

    if (m.find()) {
        int value = Integer.parseInt(am.group(1));
    }

您可以更改web服务,而不是此逻辑,只从它们的web服务中获取编号,因为这不是一个好的逻辑。是否有其他方法不修改服务器?您的问题是否已解决..假设22喜欢如何split@RajeshLawrance:如果您有静态模式,如2个喜欢,33333个喜欢,23293232个喜欢,。。。然后用我的答案,这会给出你在o索引上的喜欢数,在吐字符串的基础上space@RajeshLawrance:你现在遇到了什么问题?有时候会是这样。该怎么办?@RajeshLawrance:看我的编辑答案。在java中有很多方法可以使用字符串。只需在javayes中搜索字符串操作文本总是这样,在数值后加空格假设出现空文本视图,该怎么办?方法索引(字符串)对于.index行中的类型字符串是未定义的。我得到了这样的错误方法索引(字符串)对于.index行中的类型字符串未定义。我得到如下错误–TextView like=(TextView)rll.findViewById(R.id.like);like.setText(“22个like”);有时像.setText(“2 like”);-
     String txt = textview.getText();
     Pattern p = Pattern.compile("^([0-9]+).*");
     Matcher m = p.matcher(txt);

    if (m.find()) {
        int value = Integer.parseInt(am.group(1));
    }