LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-02-2004, 10:28 AM   #1
caged
Member
 
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Rep: Reputation: 30
using java to find the location of the "\" char in a string


i want to make a helper method in java that searches through a string and outputs the last place in the string where it finds the "\" charector. its tricky cos "\" is used in a java string to signal that an operation is being ordered eg"\n" is a break line command.

searching for "\" has no sucess neither does looking for "\\".

i have though about breaking the string into an array of type char but this is gonna slow down my program and its a bit ugly. any ideas how i can get around this easier.

cheers.
Ben.
 
Old 02-02-2004, 11:49 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
If you want to use a backslash in a string it has to be escaped anyway like \\ so

char backslash='\\';

String testString=new String("123\\56\n78\\9");

System.out.println("\\ Appears last in position " + testString.lastIndexOf(backslash));

will return an index of 9 (return index starting at zero)

Is this what you mean?
 
Old 02-02-2004, 07:11 PM   #3
caged
Member
 
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Original Poster
Rep: Reputation: 30
on the same track but well what i want this part of the program to do is...

take a location like "C:\documents and settings\ben\desktop\name" and subString the string so that only "\name" is left. the location is provided in this case from a DOS cmd piping dir into a text file and then java opens the text file and formats it and outputs an index.html file.
Code:
set classpath = 
dir > temp.txt
copy c:\code\FormatText.class FormatText.class
java FormatText.class
del temp.txt
del FormatText.class
^thats not all the code but you get the idea

since i cant change the string provided from the DIR command i could pipe the current directories name into the file before the DIR command. are there any prebuilt methods in java so i can easily insert the current directories name as a string.

Linux is obviously better suited to cope with this problem but i want my program to work on both platforms.

cheers.
Ben.

Last edited by caged; 02-02-2004 at 07:14 PM.
 
Old 02-02-2004, 10:56 PM   #4
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
how about this:


String str = "c:\\salaam\\folder";
System.out.print(str.lastIndexOf('\\'));
str = str.substring(sd.lastIndexOf('\\'));
System.out.print(str);

Last edited by Mohsen; 02-02-2004 at 10:58 PM.
 
Old 02-03-2004, 01:04 AM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Or take a look at the File class which has methods for returning the file name part, path name part etc. as a string
 
Old 02-03-2004, 05:47 AM   #6
caged
Member
 
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Mohsen
how about this:


String str = "c:\\salaam\\folder";
System.out.print(str.lastIndexOf('\\'));
str = str.substring(sd.lastIndexOf('\\'));
System.out.print(str);
yeah that would work but first i would have to change "C:\salaam\folder" into "c:\\salaam\\folder" as far as i know that would also require finding the "\" charactor and turning that into a "\\" charactor or charactors. do you know or have an idea how i could just pass "\\"?
Quote:
originally posted by Looking_Lost
Or take a look at the File class which has methods for returning the file name part, path name part etc. as a string
thats not a part of java i know anyhting about im researching at the moment thanks dude.

cheers.
Ben
 
Old 02-03-2004, 05:59 AM   #7
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You only need to escape backslashes in a string literal, not if you've got the string from some other source.

Alex
 
Old 02-03-2004, 06:17 AM   #8
caged
Member
 
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Original Poster
Rep: Reputation: 30
ok in this case i got the string from: (pseudo)
Code:
readln of file input.txt into string blahBlah
if blahBlah.indexOf("Directory")  is not equal to NULL
then (
   a String called "title" is equal to substring(blahBlah + 13);
)
end if
so the "\" charactors in the string title are not implying to the interpretor that a formatting control/escape char is next????

cheers.
Ben.
 
Old 02-03-2004, 10:42 AM   #9
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Well in that pseudocode title is a literal string (although it's hard to tell form pseudocode), so yes any \s in it will be seen as escape characters. But if you read in a string from a file, say, which contains \s, those \s won't be treated as escapes. I thought you might be getting confused over that, I'm sorry to mention it if you weren't.

Alex

Last edited by llama_meme; 02-03-2004 at 10:44 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 03:47 AM
I cannot use "java chat". Browser says plugin required "x-java-vm". jdruin Linux - Software 4 04-18-2004 05:44 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM


All times are GMT -5. The time now is 09:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration