String Multiline - Android - android

i got this issue and i don't know how to solve it. Here is the problem:
1 - i have a data in my database who i split into a strings[] and then i split this strings[] into another 2 strings[] (even and odd lines). Everything works fine but when i want to join all the lines into a single String i got a multi line string intead of a single line. Someone can help me?
data
abcdef//
123456//
ghijkl//
789012
code:
String text = "";
vec1 = data.split("//"); //split the data
int LE = 0;
for (int a = 0; a < vec1.length; a++) { //verify how many even and odds line the data have
if (a % 2 == 0) { //if 0, LE++
LE++;
}
}
resul1 = new String[LE];
int contA = 0, contB = 0;
for (int c = 0; c < resul1.length; c++) {
if (c % 2 != 0) {
text += " " + resul1[c].toLowerCase().replace("Á","a").replace("Ã","a").replace("ã","a").replace("â","a").replace("á","a").replace("é","e").replace("É","e")
.replace("ê","e").replace("í","i").replace("Í","i").replace("ó","o").replace("Ó","o").replace("õ","o").replace("Õ","o").replace("ô","o").replace("Ô", "o")
.replace("Ú","u").replace("ú","u").replace("ç","c").replace("_","").replace("<","").replace(">","");
contA++;
}
}
And the String looks like
abcdef
ghijkl
instead of
abcdefghijkl

You should use replaceAll() method.
text.replaceAll("\\r\\n|\\r|\\n", ""); // the method removes all newline characters

Related

how to start count upto 1 until press comma(,) it should count tow

its may be silly but am confused on that this i want to start count up to one and if press comma(,)then i want to count comma only, here how i am try.
String conCount;
conCount = "1";
int countComma = conCount.length() - conCount.replace(",", "").length();
String lenVar;
lenVar = conCount;
convert = String.valueOf(countComma);
if (conCount.length() == 0) {
lenVar = "0";
} else {
textViewConCount.setText(convert);
}
String editTextString = "abc,efg,pqr,xyz";
if (editTextString.contains(",")) {
int countStringsSeperatedByComma = 0;
countStringsSeperatedByComma = editTextString.split(",").length;
System.out.println("Count of strings seperated by comma : " + countStringsSeperatedByComma);
int commaCount = countStringsSeperatedByComma - 1;
System.out.println("Count of commas : " + commaCount);
} else {
System.out.println("Count of characters in editText string : " + editTextString.length());
}
Output for above condition will be :
Count of strings seperated by comma : 4
Count of commas : 3
Suppose if your string is "abcefgpqrxyz" i.e. without comma then it will execute else part and print characters count as 12 in this case
Count of characters in editText string : 12
Your question statement is so ambiguous. Elaborate it completely and explain your end result with example. It's regarding string functions, I can give you the answer about it if I understand it :) :D
thanks for that i got that answer like that.
String varStr = editextContact.getText().toString();
//int VarCount = editextContact.getText().length();
int countStringsSeperatedByComma = varStr.split(",").length;
String convet=String.valueOf(countStringsSeperatedByComma);
textViewConCount.setText(varStr);
if (varStr.length() == 0){
textViewConCount.setText("0");
}else {
textViewConCount.setText(convet);
}

format a string with fill places

I have setup an edittext box and set the maxlength to 10. When I copy the edittext to a string myTitles. I need the myTiles to be 10 chars long and not dependent on what is entered in the edittext box.
myTitles[0] = TitlesEdit.getText().toString();
The edittext was filled with ABCD so I need to add 6 spaces or placeholders after the ABCD. I have seen other post with str_pad and substr without success
myTitles[0] = str_pad(strTest, 0, 10);
myTitles[0] = substr(strTest,0, 10);
Try something like
public static String newString(String str) {
for (int i = str.length(); i <= 10; i++)
str += "*";
return str;
}
This will return a String with * replaced for the empty ones.
So, for eg, if your String is abcde, then on calling newString() as below
myTitles[0] = newString("abcde");
will return abcde***** as the output.
String s = new String("abcde");
for(int i=s.length();i<10;i++){
s = s.concat("-");
}
Then output your string s.
Thank you Lal, I use " " to fill and it worked fine. here is my new code.
String strTest = TitlesEdit.getText().toString();
for (int i = strTest.length(); i <= 10; i++) {
strTest += " ";
}
Log.d("TAG", "String" + strTest);
myTitles[intLinenumber] = strTest;

Android : Remove last paragraph tag generated by Html.toHtml

I'm editing content that may be html in an EditText. Just before saving the result, I use Html.toHtml to convert the input into a string to be sent to the server. However this method call seems to be generating paragraph tags which I dont need. Eg -
Test edited
seems to get converted to
<p dir="ltr">Test edited</p>
I would like to strip out the last paragraph tag before saving the content. If there are other paragraph tags, I would like to keep those. I have this regex that matches all p tags
"(<p.*>)(.*)(</p>)";
but I'm not sure how to match just the last paragraph and remove just the tags for it.
public static void handleOneParagraph(SpannableStringBuilder text) {
int start = 0;
int end = text.length();
String chars1 = "<p";
if (end < 2)
return;
while (start < end ) {
String seq = text.toString().substring(start, start + 2);
if (seq.equalsIgnoreCase(chars1))
break;
start++;
}
if (text.toString().substring(start, start + 2).equalsIgnoreCase(chars1) ) {
int start2 = start + 1;
String chars2 = ">";
while (start2 < end && !text.subSequence(start2, start2+1).toString().equalsIgnoreCase(chars2) ) {
start2++;
}
if (start2 >= end)
return;
text.replace(start, start2+1, "");
end = text.length();
start = end;
String chars3 = "</p>";
while (start > start2 + 4) {
String last_p = text.subSequence(start - 4, start).toString();
if (last_p.equalsIgnoreCase(chars3) ) {
text.replace(start - 4, start, "");
break;
}
start--;
}
}
}
And now, you can use it like this...
SpannableStringBuilder cleaned_text = new SpannableStringBuilder(Html.toHtml(your_text));
handleOneParagraph(cleaned_text);

Displaying values to a TextView

hi i have problem in displaying a value into my TextView..
For example i will input 1,2,3,4 then i like to display the output in this manner in my TextView..How can i do that? please help me, thank you in advance
1 appeared 1 times
2 appeared 1 times
3 appeared 1 times
4 appeared 1 times
here's my code:
String []values = ( sum.getText().toString().split(","));
double[] convertedValues = new double[values.length];
Arrays.sort(convertedValues);
int i=0;
int c=0;
while(i<values.length-1){
while(values[i]==values[i+1]){
c++;
i++;
}
table.setText(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
table.setText(values[i] + " appeared " + c + " times");
Make your textView to support multipleLines and after that create in code a StringBuffer and append to it the results, something like
resultString.append(result).append(" appeared").append(c).append(" times\n");
after that you set text for textView like:
textView.setText(resultString.toString());
Here is the idea :
// this is test string, you can read it from your textView
String []values = ( "2, 1, 3, 5, 1, 2".toString().split(","));
int [] intValues = new int[values.length];
// convert string values to int
for (int i = 0; i < values.length; ++i) {
intValues[i] = Integer.parseInt(values[i].trim());
}
// sort integer array
Arrays.sort(intValues);
StringBuilder output = new StringBuilder();
// iterate and count occurrences
int count = 1;
// you don't need internal loop, one loop is enough
for (int i = 0; i < intValues.length; ++i) {
if (i == intValues.length - 1 || intValues[i] != intValues[i + 1]) {
// we found end of "equal" sequence
output.append(intValues[i] + " appeared " + count + " times\n");
count = 1; // reset count
} else {
count++; // continue till we count all equal values
}
}
System.out.println(output.toString()); // prints what you extected
table.setText(output.toString()); // display output

Parse big xml file in android

In my application there is a search option. If the user enters a search value, I have to get a value from the webservice. I am getting a large webservice value. In my webservice string values are coming. I am getting like <> like xml character entity reference like. I want to replace all characters and parse xml. Can anybody tell me how to do this and give an example?
I tried with StringBuffer for unescapexml character, I am getting out of memory error
public String unescapeXML(String str) {
if (str == null || str.length() == 0)
return "";
StringBuffer buf = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (c == '&') {
int pos = str.indexOf(";", i);
if (pos == -1) { // Really evil
buf.append('&');
} else if (str.charAt(i + 1) == '#') {
int val = Integer.parseInt(str.substring(i + 2, pos), 16);
buf.append((char) val);
i = pos;
} else {
String substr = str.substring(i, pos + 1);
if (substr.equals("&"))
buf.append('&');
else if (substr.equals("<"))
buf.append('<');
else if (substr.equals(">"))
buf.append('>');
else if (substr.equals("""))
buf.append('"');
else if (substr.equals("&apos;"))
buf.append('\'');
else if (substr.equals(" "))
buf.append(" ");
else
// ????
buf.append(substr);
i = pos;
}
} else {
buf.append(c);
}
}
return buf.toString();
}
I tried with stream, I am not able to do it. Can anybody give an example how to do this?
You should not parse it on your own. There are better ways - SAX or DOM.
This resource contains a lot of useful inforamtion about these both ways (and code examples too): http://onjava.com/pub/a/onjava/2002/06/26/xml.html
Take a look here in order to get more details about android included parsers :
http://developer.android.com/reference/javax/xml/parsers/package-summary.html
But make your own parser with SAX is probably the best choice in your case ;)

Categories

Resources