Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

searching string for '\'

  • 25-08-2006 12:54PM
    #1
    Closed Accounts Posts: 3,105 ✭✭✭


    hi,
    just need to search a string for the character '\'. it's a reserved char and it's giving me hassle
    can someone tell me how i can get over this.
    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 3,946 ✭✭✭mp3guy


    I think if you put it in as "\"


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Tyrrial wrote:
    hi,
    just need to search a string for the character '\'. it's a reserved char and it's giving me hassle
    can someone tell me how i can get over this.
    thanks

    What Language? In Java, the backslash is an escape character, so you need to use \\ instead.


  • Closed Accounts Posts: 3,105 ✭✭✭Tyrrial


    java indeed,
    thanks, '\\' seems to have worked


  • Moderators, Education Moderators Posts: 1,699 Mod ✭✭✭✭Slaanesh


    Hey Niall,

    What Lynchie said is fine for searching but you could run into some troubles when using regular expressions.
    	String t = "Hello how \\ are you? \\ ";
    	for(int i = 0; i <t.length(); i++)
    	{
    		if(t.charAt(i) == '\\')
    		{
    			System.out.println("\\ found");
    		}
    
    	}
    	t = t.replaceAll("\\\\",".");
    	System.out.println(t);
    

    The above example 2 backslashes in the string as expected.

    In order to replace the slahes with periods we need to use 4 slashes in the regex in order to find them. I can't remember the reason for this but if you look up regular expressions I'm sure that will explain it. Someone else will probably explain it better than me though :)


  • Moderators, Politics Moderators, Paid Member Posts: 44,305 Mod ✭✭✭✭Seth Brundle


    isn't it because to escape a slash you use a slash and do this twice for each of the slashes in your search?

    Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/ .



  • Advertisement
Advertisement