I have to add 3 dots to the end of a text in a textview. My textView is in an lazy list adapter so the content change very often.Only two lines are shown in the textview.
e.g. suppose text is
abcdefghigdddddddd
dddddddddddddddddddd
result should be
abcdefghigdddddddd
ddddddddddddddddd...
I know i should use .replace("...","");
but how to always write off the last thre words ?
My textview is as follows
holder.title.setText(Html.fromHtml(a.name));
where a.name is the data to be set.
You can add 'elipses' attributes to your TextView, and also specify that the text view is just one line. It could look like this:
<TextView
android:id="#+id/text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1" />
Just set your TextView property android:ellipsize="end" and you will get the result.
First you fetch the text in a String. Then you fetch the length of the string. Then you can fetch the index of the third character from last. The you can easily replace the three last characters with ...
All this can be done using the methods of String class in Java.
Suppose you want to display only 4 characters from the string "raviranjan", and you want to trim all other characters by replacing them with "...". The do as following:-
String str = "raviranjan";
int length = str.length();
String subString = str.substring(0, 3); // takes four characters from beginning
String str2 = subString + "..."; // Add ellipsis
Now str2 is the string you want....
make textView ellipsis and let android truncate it automatically for you .
<TextView ....... bla- bla -------
.......................
android:elipsis="true"
android:lines="num_of_lines" />
If you want to replace extra characters with ellipses, you might be able to get away with something like so:
String str = "abcdefghigdddddddd dddddddddddddddddddd";
String newStr = str.replaceAll("^(.{100})(.*)$","$1...");
The above should match the first 100 characters and throw them in one group, while throw the other characters in another group. The result will be then a new string which is made up from the first group followed by 3 dots.
You can change the value of 100 to whatever amount of characters you want before the ellipses.
you want to add 3 dots at end of your text then
holder.title.setText(Html.fromHtml(a.name+"..."));
for remove 3 char & add 3 dots..
String a = a.name;
a.substring(0, a.length() - 3);
holder.title.setText(Html.fromHtml(a.name+"..."));
Related
I have a list of names in an Adapter. When the name is being highlighted and if the name is too long (more than 2 words), I need to move the words after the gap of second word to next line in such a way that the words moved in new line maintain same space with the next name in Adapter.
The example is, consider the list of names in the list as -
. Alex
. Martin Kooper Well
. Thomas
(As you see, when first name is highlighted, since it is less than 2 words, I dont need to anything)
Now,
. Alex
. Martin Kooper
Well
. Thomas
(As you see, when second name is highlighted, since it is more than 2 words, I need to put the third word Well in next line. It should maintain same spacing with next element Thomas in the list)
Try
String name = "some person name".trim();
if(name.indexOf(" ") != name.lastIndexOf(" ")) {
int secondSpacePos = name.indexOf(" ", name.indexOf(" ") + 1);
name = name.substring(0, secondSpacePos) + "\n " + name.substring(secondSpacePos);
}
you can use Split() for this
String string = "android iPhone Windows";
String[] parts = string.split(" ");
Here you can check array size. if size/2 > 1 then you can add "\n" and add string to String buffer and then set to your listview textview
Hope this will help to you.
Split the string on bases of white space and store it in array.
Do the following stuff
String name = "Martin Kooper Well";
String[] newname = name.split(" ");
if (newname.length>2)
name = newname[0] +" " +newname[1] + "\n" +newname[2];
You should do it in your view layout. Specify enough item view's height for a 2-line textview. When there is not enough horizontal space for the bold name ( or even a very large name ) the text will split automatically to use the second line. Specifiying a height you will have all items equal.
<TextView
android:id="#+id/myText"
android:layout_height="50dp"
android:layout_width="match_parent"
android:maxLines="2"
android:lines="2"/>
As i haven't much worked on regex, can someone help me out in getting the answer for below thing:
(1)I want to remove a text say Element
(2)It may of may not followed by delimiter say pipe(||)
I tried below thing, but it is not working in the way i want:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
System.out.println(str.replaceFirst("Element.*\\||", ""));
System.out.println(str1.replaceFirst("Element.*\\||", ""));
Required output in above cases:
String:abc||Value:abc //for the first case
String:abc //for the second case
Assuming that you can decide to give another value to the original pattern which is Element in this case, you can use Pattern.quote to escape it as below:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
String originalPattern = "Element";
String pattern = String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern));
System.out.println(str.replaceFirst(pattern, ""));
System.out.println(str1.replaceFirst(pattern, ""));
Your patter is then generic and its value is String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern))
Output:
String:abc||Value:abc
String:abc
You put the escape wrong. It should be:
Element(.*?\|\||.*$)
Put the escape on each pipe, and use ? for non greedy Regex so you only replace just enough string, not everything.
String text = "String:abc||Element:abc||Value:abc";
text = text.replaceAll("\\belement\\b", "");
you might need to use replace all this will replace all element from your string here i am using '\b' word boundary in java regular expression in between the words
I am trying to split a string according to a certain set of delimiters.
My delimiters are: ,"():;.!? single spaces or multiple spaces.
This is the code i'm currently using,
String[] arrayOfWords= inputString.split("[\\s{2,}\\,\"\\(\\)\\:\\;\\.\\!\\?-]+");
which works fine for most cases but i'm have a problem when the the first word is surrounded by quotation marks. For example
String inputString = "\"Word\" some more text.";
Is giving me this output
arrayOfWords[0] = ""
arrayOfWords[0] = "Word"
arrayOfWords[1] = "some"
arrayOfWords[2] = "more"
arrayOfWords[3] = "text"
I want the output to give me an array with
arrayOfWords[0] = "Word"
arrayOfWords[1] = "some"
arrayOfWords[2] = "more"
arrayOfWords[3] = "text"
This code has been working fine when quotation marks are used in the middle of the sentence, I'm not sure what the trouble is when it's at the beginning.
EDIT: I just realized I have same problem when any of the delimiters are used as the first character of the string
Unfortunately you wont be able to remove this empty first element using only split. You should probably remove first elements from your string that match your delimiters and split after it. Also your regex seems to be incorrect because
by adding {2,} inside [...] you are in making { 2 , and } characters delimiters,
you don't need to escape rest of your delimiters (note that you don't have to escape - only because it is at end of character class [] so he cant be used as range operator).
Try maybe this way
String regexDelimiters = "[\\s,\"():;.!?\\-]+";
String inputString = "\"Word\" some more text.";
String[] arrayOfWords = inputString.replaceAll(
"^" + regexDelimiters,"").split(regexDelimiters);
for (String s : arrayOfWords)
System.out.println("'" + s + "'");
output:
'Word'
'some'
'more'
'text'
A delimiter is interpreted as separating the strings on either side of it, thus the empty string on its left is added to the result as well as the string to its right ("Word"). To prevent this, you should first strip any leading delimiters, as described here:
How to prevent java.lang.String.split() from creating a leading empty string?
So in short form you would have:
String delim = "[\\s,\"():;.!?\\-]+";
String[] arrayOfWords = inputString.replaceFirst("^" + delim, "").split(delim);
Edit: Looking at Pshemo's answer, I realize he is correct regarding your regex. Inside the brackets it's unnecessary to specify the number of space characters, as they will be caught be the + operator.
I have a String which has numbers and I want to add this sign ":" between every two numbers as if the string was 0123456789 I want it to be like this 01:23:45:67:89
Is there any way to insert it ?? as I read about replace() but this does not help in my case
You could use this magic piece of regex:
System.out.println("0123456789".replaceAll(".{2}(?!$)", "$0:"));
.{2} match 2 characters
(?!$) not at end
$0: First matched argument with : included
String x="0123456789";
String result="";
for(int i=0;i<x.length();i++){
result+=x.charAt(i);
if(i%2==1 && i+1<x.length())
result+=":";
}
Given String
// 1 2 3
String a = "letters.1223434.more_letters";
I'd like to recognize that numbers come in a 2nd position after the first dot
I then would like to use this knowledge to replace "2nd position of"
// 1 2 3
String b = "someWords.otherwords.morewords";
with "hello" to effectively make
// 1 2 3
String b = "someWords.hello.morewords";
Substitution would have to be done based on the original position of matched element in String a
How can this be done using regex please?
For finding those numbers you can use group mechanism (round brackets in regular expresions):
import java.util.regex.*;
...
String data = "letters.1223434.more_letters";
String pattern="(.+?)\\.(.+?)\\.(.+)";
Matcher m = Pattern.compile(pattern).matcher(data);
if (m.find()) //or while if needed
for (int i = 1; i <= m.groupCount(); i++)
//group 0 == whole String, so I ignore it and start from i=1
System.out.println(i+") [" + m.group(i) + "] start="+m.start(i));
// OUT:
//1) [letters] start=0
//2) [1223434] start=8
//3) [more_letters] start=16
BUT if your goal is just replacing text between two dots try maybe replaceFirst(String regex, String replacement) method on String object:
//find ALL characters between 2 dots once and replace them
String a = "letters.1223434abc.more_letters";
a=a.replaceFirst("\\.(.+)\\.", ".hello.");
System.out.println(a);// OUT => letters.hello.more_letters
regex tells to search all characters between two dots (including these dots), so replacement should be ".hello." (with dots).
If your String will have more dots it will replace ALL characters between first and last dot. If you want regex to search for minimum number of characters necessary to satisfy the pattern you need to use Reluctant Quantifier ->? like:
String b = "letters.1223434abc.more_letters.another.dots";
b=b.replaceFirst("\\.(.+?)\\.", ".hello.");//there is "+?" instead of "+"
System.out.println(b);// OUT => letters.hello.more_letters.another.dots
What you want to do is not directly possible in RegExp, because you cannot get access to the number of the capture group and use this in the replacement operation.
Two alternatives:
If you can use any programming language: Split a using regexp into groups. Check each group if it matches your numeric identifier condition. Split the b string into groups. Replace the corresponding match.
If you only want to use a number of regexp, then you can concatenate a and b using a unique separator (let's say |). Then match .*?\.\d+?\..*?|.*?\.(.*?)\..*? and replace $1. You need to apply this regexp in the three variations first position, second position, third position.
the regex for string a would be
\w+\.(\d+)\.\w+
using the match group to grab the number.
the regex for the second would be
\w+\.(\w+)\.\w+
to grab the match group for the second string.
Then use code like this to do what you please with the matches.
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
where patternStr is the pattern I mentioned above and inputStr is the input string.
You can use variations of this to try each combination you want. So you can move the match group to the first position, try that. If it returns a match, then do the replacement in the second string at the first position. If not, go to position 2 and so on...