Results 1 to 6 of 6
  1. #1
    ChukMakeIt is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Question Replace specific text with a link?

    Hai!
    I am hoping to make a Google App which does the following:
    Replaces
    XML Code:
    /watch?v=x (where x is a string of text)
    with a link to the following:
    XML Code:
    http://www.youtube.com/watch?v=x
    For example, a YouTuber may comment
    Epic Java Tutorial! /watch?v=Hl-zzrqQoSE
    and I want it to replace that with the link (in HTML):
    XML Code:
    <a href = "http://www.youtube.com/watch?v=Hl-zzrqQoSE">/watch?v=Hl-zzrqQoSE</a>
    So, does anybody know the best way to do this? I know how to do the HTML part, just not the Java.
    If anybody knows of a way to do it using another form of code (e.g. Python, C++, PHP), it will be most appreciated, too.
    Thanks
    ~Charlie
    Last edited by ChukMakeIt; 07-13-2012 at 10:50 PM.

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Replace specific text with a link?

    Have you already tried to do it in java? If you've tried please give us part of your code that is important for your question.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Replace specific text with a link?

    @ChukMakeIt:

    My advice is first try to make very easy, simple program for fun, and then later you could use that code in your App.

    1. Make two Strings for example:
    String youtubeSample = "Epic Java Tutorial! /watch?v=Hl-zzrqQoSE";
    String newYoutubeLink ="";

    2. make method for example public void findAndReplace() {}

    3. in that method try to find if your String youtubeSample contains "/watch?v=".

    4. If it contains it then you should replace occuring of old substring "/watch?v=" with new substring "http://www.youtube.com/watch?v=" in your youtubeSample string, and change newYoutubeLink value. It should be something like this:

    Java Code:
    newYoutubeLink = youtubeSample.replace("/watch?v=", "http://www.youtube.com/watch?v=");
    5. Print result of newYoutubeLink to see if it's working.

  4. #4
    ChukMakeIt is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Replace specific text with a link?

    So far, I'm thinking of doing this:
    Java Code:
    var str="[location of comment in HTML]";
    Z=var n=str.replace("/watch?v='x'","/watch?v='x'");
    
    B="http://www.YouTube.com"
    A=B+Z
    var n=str.replace("/watch?v='x'","A");
    If that will work, great, but I need to know '[location of comment in HTML]'. Any thoughts?

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Replace specific text with a link?

    I wrote simple program in Java so it might be easier for you to understand what I'm talking about:

    Java Code:
    public class YouTubber {
    	String youtubeSample = "Epic Java Tutorial! /watch?v=Hl-zzrqQoSE";
    	String newSample;
    	
    	public void findAndReplace() {
    		if(youtubeSample.contains("/watch?v=")) {
    			System.out.println("Youtube Sample: " + youtubeSample);
    			newSample = youtubeSample.replace("/watch?v=", "http://www.youtube.com/watch?v=");
    			
    			System.out.println("New Youtube Sample: " + newSample);
    		}
    	}
    	
    	public static void main(String[] args) {
    		YouTubber tube = new YouTubber();
    		tube.findAndReplace();
    	}
    }
    What do you mean by 'location of comment in HTML'? Can you give me simple example of that?

  6. #6
    ChukMakeIt is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Replace specific text with a link?

    Quote Originally Posted by cselic View Post
    What do you mean by 'location of comment in HTML'? Can you give me simple example of that?
    In the YouTube video I linked before (Java Programming Tutorial - 1 - Installing the JDK - YouTube) had a top comment, which (coincidentally) is a perfect example of my target comment:
    /watch?v=OkcKs3Gz3PY&feature=p*%C2%ADlcp
    Illegal copy of Bucky's tutorial. ".FLV" extension in title proves that it has been downloaded via YouTubeDownloader. Thumbs up so that everyone can report that person.
    I wish to replace
    /watch?v=OkcKs3Gz3PY&feature=p*%C2%ADlcp
    with a link to Java Programming Tutorial - 1 - Installing the JDK .FLV - YouTube.
    In the HTML code of the video, the YouTubbers comment has the class 'comment text', :
    XML Code:
    <div class="comment-text" dir="ltr">
              <p>/watch?v=OkcKs3Gz3PY&amp;feature=p<wbr>*%C2%ADlcp</p>
    <p>Illegal copy of Bucky's tutorial. ".FLV" extension in title proves that it has been downloaded via YouTubeDownloader. Thumbs up so that everyone can report that person.</p>
    
            </div>
    So basically, how could I locate and search the class id, and use it in your code:
    Java Code:
    public class YouTubber {
        String youtubeSample = "Epic Java Tutorial! /watch?v=Hl-zzrqQoSE";
        String newSample;
         
        public void findAndReplace() {
            if(youtubeSample.contains("/watch?v=")) {
                System.out.println("Youtube Sample: " + youtubeSample);
                newSample = youtubeSample.replace("/watch?v=", "http://www.youtube.com/watch?v=");
                 
                System.out.println("New Youtube Sample: " + newSample);
            }
        }
         
        public static void main(String[] args) {
            YouTubber tube = new YouTubber();
            tube.findAndReplace();
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 07-04-2012, 09:01 PM
  2. Replies: 6
    Last Post: 05-29-2011, 01:14 PM
  3. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 03:38 AM
  4. Link to specific part of a Java Servlet
    By jwilley44 in forum Java Servlet
    Replies: 2
    Last Post: 02-03-2009, 02:51 AM
  5. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •