Splitting array not working in android - android

I am splitting a string with some sample text in the middle. But it is not splitting. Please help me where i went wrong
String[] str;
parts[1] = "loopstWatch out this is testingloopstThis makes the difference";
str = parts[1].trim().split("loopst");

String[] arr;
arr = "loopstWatch out this is testingloopstThis makes the difference".trim().split("loopst");
Log.e("Data arr[0]",":"+arr[0]);
Log.e("Data arr[1]",":"+arr[1]);
Log.e("Data arr[2]",":"+arr[2]);
Output
arr[0] :""
arr[1] :"Watch out this is testing"
arr[2] :"This makes the difference"

I think you're doing it backwards.
You want to first split, then get the piece at index 1, then trim, then set str equal to the result:
String s = "loopstWatch out this is testingloopstThis makes the difference";
String str = s.split("loopst")[1].trim();
// str should be equal to "Watch out this is testing"

Related

How to truncate decimal value in a text

So, I need to show a string in UI which has both numbers and text together.
Something like this,
10.289 Mbps
and I wanted to remove .289 and just show 10 Mbps
I tried a lot of options like setting text as
String rounded = String.format("%.0f", speedValue);
But nothing seems to be working for me.
Appreciate any help.
This can be possible in many ways.
Split String
String inputStr = "10.289 Mbps";
String[] splited = inputStr.split(" ");
Double val = Double.parseDouble(splited[0]);
System.out.println("Value : "+val.intValue()+" "+splited[1]);
Regx
Pattern pattern = Pattern.compile("(([0-9]+)(.)?([0-9]+)?) ([A-Z,a-z]+)", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.find())
{
System.out.println("Value : "+matcher.group(2)+" "+matcher.group(5));
}
something like this can work:
string = "10.289 Mbps"
string_list = string.split(" ")
number = string_list[0]
number = float(number)
print(round(number))
basically isolate the number bu removing 'Mbps' then cast it to a float value and round it to get an integer.
try this
String str = "10.289 Mbps";
String[] strArray = str.split(" ");
Long roundedValue = Math.round(Double.valueOf(strArray[0]));
String resultStr = roundedValue.toString() + " " + strArray[1];
System.out.println(resultStr);

How to Split a string by Comma And Received them Inside Different EditText in Android?

I have Two EditText(id=et_tnum,et_pass). I Received a String like 12345,mari#123 inside EditText1(et_tnum) . I want to Split them by Comma and After Comma i should Receive Remainder string into EditText2(et_pass). Here 12345,mari#123 is Account Number & Password Respectively.
String[] strSplit = YourString.split(",");
String str1 = strSplit[0];
String str2 = strSplit[1];
EditText1.setText(str1);
EditText2.setText(str2);
String CurrentString = "12345,mari#123";
String[] separated = CurrentString.split(",");
//If this Doesn't work please try as below
//String[] separated = CurrentString.split("\\,");
separated[0]; // this will contain "12345"
separated[1]; // this will contain "mari#123"
There are other ways to do it. For instance, you can use the StringTokenizer class (from java.util):
StringTokenizer tokens = new StringTokenizer(CurrentString, ",");
String first = tokens.nextToken();// this will contain "12345"
String second = tokens.nextToken();// this will contain "mari#123"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method
This answer is from this post
You can use
String[] strArr = yourString.split("\\,");
et_tnum.setText(strArr[0]);
et_pass.setText(strArr[1]);
Try
String[] data = str.split(",");
accountNumber = data[0];
password = data[1];

Split string in android

I'm trying to split a string. For example "Castle"
String text = "Castle";
String[] word = text.split("c");
Log.d(Constants.RESULT, "1:" + word[word.length-1]);
I expect the result to be (1:astle). Instead i get (1:Castle)
I hope you guys cal help me out. Thanks!
Use String[] word = text.split("C");
Simple thing is that the Alphabet 'c' is not same as 'C',You are using 'c',better use 'C',
Try this
String text = "Castle";
String[] word = text.split("C");
Log.d(Constants.RESULT, "1:" + word[word.length-1]);
use this:
String text = "Castle";
String[] word = text.split("C");
Log.d(Constants.RESULT, "1:" + word[1]);
you got "astle" from this
Use
String text = "Castle";
String sub =text.substring(1,text.length);
Log.d( "Answer", "1:" + sub);

TalkBack decimal announcements

I'm having a problem with Talkback. In my string I have the following number.
1,827
it's a Dutch number so the number means:
1 euro and 82,7 cents.
But Talkback is saying:
18 hundred and 27 euro.
So this problem only occurs when I have a number with more than 2 decimals. How to fix this issue?
---EDIT---
When I'm reading this page: https://english.stackexchange.com/questions/62397/reading-out-decimal-numbers-in-english
it seems I must pronounce the number 1,827 as the following:
one point eight two seven instead to act if it is a number. How to do this?
---EDIT---
I included the following:
StringTokenizer stringTokenizer = new StringTokenizer(value, ".");
String currencyBeforeComma = null;
String currencyAfterComma = null;
while (stringTokenizer.hasMoreTokens()) {
currencyBeforeComma = stringTokenizer.nextToken();
currencyAfterComma = stringTokenizer.nextToken();
}
result = currencyBeforeComma + " point " + currencyAfterComma;
So now its pronouncing: one point eighthunderd twentyseven. So this is still now what i want.
---EDIT---
String value = 1,827;
String result = "";
for (Character chars : value.toCharArray()) {
result = result + chars + " ";
}
String replace = result.replace(getResources().getString(R.string.accessibility_decimal_separator), getResources().getString(R.string.accessibility_decimal_separator_text));
return replace;
This is what i did. I created a whitespace for every char in the value string and for the , or . I replaced it with a comma or point text.
This works for now but it isn't a clean solution. If anybody else has a better solution, please share
Try this and modify your as per your requirement
TextView currencylbl = (TextView) findViewById(R.id.currencylbl);
boolean countryCurrency = true;
String currency = "1,827";
if(countryCurrency==true && currency.contains(",")){
StringTokenizer stringTokenizer = new StringTokenizer(currency, ",");
String currencyBeforeComma = null;
String currencyAfterComma = null;
while (stringTokenizer.hasMoreTokens()) {
currencyBeforeComma = stringTokenizer.nextToken();
currencyAfterComma = stringTokenizer.nextToken();
currencylbl.setText(currency);
}
Double double1 = Double.parseDouble(currencyAfterComma);
double1 = Double.parseDouble(currencyBeforeComma)+double1/1000;
currencylbl.setContentDescription(double1+"");
}
countryCurrency refers to particular Country currency boolean.
Also modify as per your requirement.
Double double1 = Double.parseDouble(currencyAfterComma);
double1 = Double.parseDouble(currencyBeforeComma)+double1/1000;
currencylbl.setContentDescription(double1+"");
xml is
<TextView
android:id="#+id/currencylbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
/>

java delimeter replacement for new line

Here is the scenario one input
String in = "<ENTER>title=Java-Samples<ENTER>" +
"<ENTER>author=Emiley J<ENTER>" +
"<ENTER>publisher=java-samples.com<ENTER>" +
"<ENTER>copyright=2007<ENTER>" +
"<ENTER>cool beans<ENTER>";
processs
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
String mFinal="";
for(int i=0;i<mSplitted.length;i++)
{
System.out.println("values: "+mSplitted[i]);
mFinal= mFinal+ mSplitted[i];
}
System.out.println(mFinal);
output is
title=Java-Samplesauthor=Emiley Jpublisher=java-samples.comcopyright=2007cool beans
Senario 2
input
String in = "What is the output of: <ENTER><ENTER>echo 6 % 4;";
processes
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
String mFinal="";
for(int i=0;i<mSplitted.length;i++)
{
// System.out.println("values: "+mSplitted[i]);
mFinal= mFinal+ mSplitted[i];
}
System.out.println(mFinal);
Output
nothing
I want the output to add a new line when is used
Still not 100% on your question but how about this:
String mFinal = in.replaceAll("<ENTER><ENTER>", "\n").replaceAll("<ENTER>", "");

Categories

Resources