Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

searching string for '\'

Options
  • 25-08-2006 1: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 Posts: 3,969 ✭✭✭mp3guy


    I think if you put it in as "\"


  • Registered Users Posts: 1,996 ✭✭✭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,863 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 Posts: 39,812 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?


  • Advertisement
Advertisement