capitalize special words(abbrevations) in android - android

I want to capitalize first word of abbreviations like dr. mr. etc.
search over the internet but didn't find any useful thing about it.
i tried
String capitalized = WordUtils.capitalize(sb.toString(), '.', ' ');
System.out.println(capitalized);
but it capitalize first word of every sentence.
Thanks in advance.

Make a regex that could find title like mr. dr., etc.. RegEx: Match Mr. Ms. etc in a "Title" Database field
Prepare an array with indexes that need to be capitalised.
Create utility method that does the conversion process. Capitalize various letters in a string

Related

Regex to remove all special characters except periods

I need help with creating a regex that removes all special characters, including commas, but not periods. What I have tried to do is escape all the characters, symbols and punctuation I do not want. It is not working as intended.
replace("[-\\[\\]^/,'*:.!><~##\$%+=?|\"\\\\()]+".toRegex(), "")
I removed the period and tested that too. It did not work.
replace("[-\\[\\]^/,'*:!><~##\$%+=?|\"\\\\()]+".toRegex(), "")
For example, lets take the String "if {cat.is} in a hat, then I eat green eggs and ham!".
I want the result
if {cat.is} in a hat then I eat green eggs and ham (comma and exclamation symbol removed)
Note: I want to keep brackets, although braces are OK to omit.
Anyone have a solution for this?
You can use
"""[\p{P}\p{S}&&[^.]]+""".toRegex()
The [\p{P}\p{S}&&[^.]]+ pattern matches one or more (+) punctuation proper (\p{P}) or symbol (\p{S}) chars other than dots (&&[^.], using character class subtraction).
See a Kotlin demo:
println("a-b)h.".replace("""[\p{P}\p{S}&&[^.]]+""".toRegex(), ""))
// => abh.

Regex with numbers and alphabets mandatory, special characters optional

In my application edittext value need at least one digit and one alphabet is mandatory, and some special characters are optional i.e ".-", like any whare in the string.
For example ram123-. or r_m-12.m or .--ram123 or ram123.-_.
For this I need regex. I have already tried with this one
str_userId.matches("[A-Za-z0-9]*+[?.?_?-]")
But not working. Here how to add special characters are optional.
Thanks, In Advance
You could use a positive lookahead (?= to assert at least one occurrence of a-z and after that match at least a single digit [0-9].
Before and after matching the digit, you could add the . _ and - to the character class [A-Za-z._-]* and repeat it 0+ times.
Note that a character class matches on of the listed characters. This notation [?.?_?-], which could be written as [?._-] would also match a question mark instead of making the others optional
^(?=[^a-z\n]*[a-z])[A-Za-z._-]*[0-9][A-Za-z0-9._-]*$
Regex demo

Preserve text case when replacing based on case-insensitive search?

In my app I have a Textview with some text. I'm trying to get an input from the user, and then highlight words in the Textview according to that input.
For instance if the text is
Hello stackoverflow
and the input for the user is
hello
I want to replace the text with:
<font color='red'>Hello</font>` stackoverflow
This is my code:
String input = //GETTING INPUT FROM THE USER
text= text.replaceAll(input,"<font color='red'>"+input+"</font>");
Textview.setText(Html.fromHtml(text));
And the replacement is working, but the problem is that my current code changes the original word cases, for example :
Text: HeLLo stackoverflow
Input: hello
What i get: <font color='red'>hello</font> stackoverflow
What i want: <font color='red'>HeLLo</font> stackoverflow
You have to think about regular expressions.
replaceAll allows you to use regular expressions, and so, you can replace the text for the exact occurrence that was found.
For instance if Hello was found, it replaces it for <font color='red'>Hello</font>.
If HeLLo is found, it replaces it for <font color='red'>HeLLo</font>
Your code should be somehing as easy as:
String highlighted = text.replaceAll("(?i)("+input+")","<font color='red'>$1</font>");
This means:
(?i) : i want to search for something, case insensitive
"("+input+")" : input is betwen ( and ) because we are creating a group, so this group can be refered later
"<font color='red'>$1</font>" : instead of replacing by input, that would change the case, we replace it by `$1, that is the reference to the first matched group. This means that we want to replace it using the exact word that was found.
But please, try it and keep playing since regular expressions are tricky.
Other reads
It is easier and more clear if you use the Patternclass.
You can read more here:
http://developer.android.com/reference/java/util/regex/Pattern.html
Also, you can take a look at how to do it:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29
public String replaceAll(String regex, String replacement)
.
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.
Parameters:
regex - the regular expression to which this string is to be matched
replacement - the string to be substituted for each match
Returns:
The resulting String
UPDATE
You can test your regular expressions in this page:
http://www.regexplanet.com/advanced/java/index.html

R.id value requirements

I am currently working on an app, and I have noticed that if I assign any element (e.g. Textview) a numeric id value (such as android:id="#+id/1") - I get an error and it will not compile until I add a letter to the id.
My questions are:
1) Why are we not able to use numeric values?
2) Are there any other requirements of R id's?
Just trying to better understand the logic behind this..
I have tried searching with not much luck...
Thanks
taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_"
i believe this goes the same for R.id's. meaning they have to start with a letter, "$" or "-".
for your second question , the convention for R.id is that it should be all lower case and no spaces between words just underscore "_".
To my understanding it is just Java convention. For example, you wouldn't call a variable "1" you would call it "one". It is so the compiler can differentiate between numbers and strings. I recommend just labeling the ids based on what it is going to do, makes it easier on you.
Variables can only start with letters or underscores. Other than that, there are no requirements.

EdiText Android InputType

Looking on input types that a EditText view can have i seen "textPersonName" and i try to search a way to (if possible) split name and surname using maybe a method like editText.getName. My question is exist a method of edittext that splits name and surname in edittext? And if don't exist the best way is to use split or else?
The textPersonName is just a style option, for instance it capitalizes the first letter of each word. It won't help you distinguish first and last names. However you can search for spaces, for instance:
String[] names = editText.getText().toString().split(" ");
The Array of Strings will have each name in a different String. If the user entered: John Doe you would have "John" in names[0] and "Doe" in names[1].

Categories

Resources