Skip to main content

Pull links from string in Android

Sample code:

String Message="http://indianexpress.com/article/sports/football/mexico-holds-host-brazil-to-0-0-draw-at-world-cup/ http://indianexpress.com/article/sports/cricket/suresh-raina-surprised-by-stuart-binnys-performance/ http://indianexpress.com/article/sports/football/fifa-world-cup-every-match-is-like-a-final-for-us-says-andres-iniesta/"

ArrayList<String> forrmatted=pullLinks(Message);
 for(int i=0;i<forrmatted.size();i++){
      String temp=forrmatted.get(i).toString();
      Message=Message.replace(temp, "["+temp+"]");
}

private ArrayList<String> pullLinks(String text) {
 ArrayList<String> links = new ArrayList<String>();
 String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
 Pattern p = Pattern.compile(regex);
 Matcher m = p.matcher(text);
 while(m.find()) {
    String urlStr = m.group();
    if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
    urlStr = urlStr.substring(1, urlStr.length() - 1);
   }
links.add(urlStr);
}
return links;
}

1.Message string contains three links.
2.Create pullLinks method to pull links from Message string.
3. In pullLinks method create regular expression for links.
4. Now check pattern from input using while loop.
5. And return arraylist of type string.
6. In formatted arraylist we get links.


There is another way to check string contain valid URL or not:

if(!URLUtil.isValidUrl(URL)){
System.out.println("Valid");
}else{
System.out.println("Invalid");
}

If you want to check single URL then above method is best.
But if you want to pull multiple links from string then use pullLinks method.

Comments

Popular posts from this blog

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...

How prevent the Android OS to take a screenshot of the app when it's being pushed into the background for security reasons

How to create custom title in Android Application

Custom title is nothing but a layout you bind to your application's title bar. MainActivity.java package com.example.customtitlebar; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Window; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customheader); init(); } private void init() { // TODO Auto-generated method stub Button Left=(Button)findViewById(R.id.header_left_btn); Left.setText("BACK"); Left.setTextColor(Color.GRAY); Button Title=(Button)findViewById(R.id.header_text); Title.setText("Scorecard"); Title.setTextColor(Color.GRAY); Button Right=(Button)findViewById(R.id.header_right_...