using java to find the location of the "\" char in a string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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????
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.