android: divide string with punctuation and maintain punctuation with sentence in array - android

I have a sentence,
hello, What you are doing?How are you?
I wanted to split the sentence with characters such as .,?
I have achieved it using split function
and the output is:
arr[0]=hello
arr[1]=What you are doing
arr[2]=How are you
but I want the array as
arr[0]=hello
arr[1]=,
arr[2]=What you are doing
arr[3]=?
arr[4]=How are you
arr[5]=?
I have done this code
String text = "hello, What you are doing?How are you?";
String[] arr= text.split("[\\.,!;?:\"]+");
for (String str : arr) {
System.out.println(str);
}
Please help.

The general strategy to preserve text while also splitting on it at the same time is to use lookarounds. Lookarounds assert logic, but do not actually consume any text. They are basically zero width. I split below using the pattern:
(?=[\\.,!;?:\"])|(?<=[\\.,!;?:\"])
This says to split if we lookahead or lookbehind and see a punctuation character.
String sentence = "hello, What you are doing?How are you?";
String[] parts = sentence.split("(?=[\\.,!;?:\"])|(?<=[\\.,!;?:\"])");
for (String part : parts) {
System.out.println(part);
}
hello
,
What you are doing
?
How are you
?
Demo
You might also want to run String#trim on each term to get the exact output you want.

Related

How can I find Android TextView number of characters per word?

How can I find how many characters are there in each word of a TextView in Android. I want to set few words of my TexView to bold type. So for instance if the text view has a text "Happy Coding, Fellow Coders". I would like to set "Coding" & "Coders" to bold type.
Try this solution. It contains counting words based on the comma, colon or semicolon and dot characters :
String str = "Happy Coding, Fellow Coders";
String[] strArr = str.split(",| |.");
Log.e("TAG", "split String: " + strArr);
If you want to split only from whitespaces, then use :
.split("\\s+") instead of .split(" ")
Then you can simply use the loops to count the characters of every words.

is it possible to cut text from string like this?

I want to put extra value from intent to other intent. But in other intent, app get all value. Example:
mAddress.setText(" from " + address);
String put_address = mAddress.getText().toString();
editIntent.putExtra("put_address", put_address);
is it possible to cut text "from" and get only address variable ???
you can split a string like
str = "From address#dd.com";
String modified = str.replace;
now splitstr contain your split strings
splitStr[1] contains "address#dd.com"
Can also use
str.substring(str.indexOf(" ")+1);
By the way, you can use jagapathi's answer. In his example he uses regular expression.
Regular expressions can help to parse, find, cut substrings using a particular pattern. In his code he splits string by any space character.
But, imho, the simplest solution is to create a substring using this code:
'put_address.substring(7);'
use one of these solutions:
String input = put_address.trim().substring(5);
*** note: 5 is index of real address first character;
String input = put_address..split(" ")[1];

trim string to a specific character

I want to trim a specific String till a specific character. So the String: com.icecoldapps.screenshoteasy for example shall be the String screenshoteasy. But larger Strings like com.google.android.syncadapters.contacts shall also be trimed to contacts.
How to do that?
Thanks
String yourString = "com.google.android.syncadapters.contacts";
String[] sArr = yourString.split("\\.");
String output = sArr[sArr.length-1];
You can use the split method to return a array with the string split into parts on a delimiter.
String whole = "com.icecoldapps.screenshoteasy"
String[] parts = whole.split('.');
So parts would be ["com" , "icecoldapps", "screenshoteasy"]
You can also split using more complicated strings of characters. Might be worth looking at Regular Expressions and the API for that.

How can I make textview.Contain search for word?

currently I am developing a basic AI that responds to user input.
I have this line of code:
if (value.contains("hi")){
test.setText("Hello.");
speakOut();}
}
But this line is very easily triggered with words that are not "hi", such as this, which also contains "hi".
What code can I use to search for exact words rather than certain characters?
Then use the equals method :
"hi".equals(value)
If you have a sentence with the word hi, you could just split it :
String s = "Hi I'm this";
String [] words = s.split("\\s+"); //split by whitespaces
loop: for(String word : words){
if("hi".equals(word.toLowerCase())){
System.out.println("hello");
break loop;
}
}
Like the comments stated, you may have a sentence like "Hi, blablabla" and this won't work. But now that you have the idea you will be able to find a good regex that will catch only words.

Android removing newline characters

TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('\n');
splitter.setString(tweetMessage);
tweetMessage = "";
for (String s : splitter)
{
Log.v(getClass().getSimpleName(), "Spliter :: "+ s);
tweetMessage +=s;
}
But this shows up the newline characters as Squares how do I get Rid of that
The characters you're seeing as squares probably aren't '\n' ('\u000A') characters.
If you're seeing a multiple lines in your output then it's possible that multiple characters are being used to split the lines. It might be '\r\n' but it could be something else.
Your best bet is to use a debugger to check exactly what the String contains before you split it. Or you could add some debug code like so:
for (int u : tweetMessage.toCharArray()) {
Log.v(getClass().getSimpleName(), String.format("\\u%04X",u));
}
If you see \u000D \u000A then your lines are separated by "\r\n". This means you can't use a SimpleStringSplitter to split the string since that will only split on a single character.
If you don't want to parse the string manually, you could use the String.split() method:
for (String s : tweetMessage.split("\r\n") {
//do something with s
}
If you need to remove newline/tab characters at the beginning and the end of a string (not the ones in the middle of the string) just use .trim() function to do the tric. This will remove white spaces from the begging and the end of the string.
Ex:
String xmlVersionDate = rootNode.getChildNodeWithName("last-updated").getValue().trim();

Categories

Resources