Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Check if there are numbers in a string in Java

edited March 14 in Android

I am developing a webView android application that embedded actual a Vanilla forum.I want to interact from one post to another using button at the bottom of NavigationView.I want to read the current url format it and open a new url in previous and next buttons.

Sample url =http://mafisizone.com/discussion/72/why-is-running-best-exercise-to-loose-weight

How do i read that url and get only the number 72.

Help guys

Comments

  • Actually, i was able to work this out.Below code worked fine for me
    `/**
    *
    * @author Eric
    * site:techoverload.net
    */
    public class StringExample {

    public StringExample()
    {
    String s=extractNumber("http://mafisizone.com/discussion/150/join-mafisi-zone-telegram-channel#latest");
    int i=Integer.parseInt(s);
    System.out.println(i);
    
    
    }
    
    
    public  String extractNumber( String str) {                
    
    if(str == null || str.isEmpty()) return "";
    
    StringBuilder sb = new StringBuilder();
    boolean found = false;
    for(char c : str.toCharArray()){
        if(Character.isDigit(c)){
            sb.append(c);
            found = true;
        } else if(found){
            // If we already found a digit before and this char is not a digit, stop looping
            break;                
        }
    }
    
    return sb.toString();
    

    }

    public static void main(String args[])
    {
    

    new StringExample();
    }

    }
    `

Sign In or Register to comment.