Home arrow static arrow Java Programming [Archive] - String substring
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String substring
This topic has 7 replies on 1 page.

Posts:93
Registered: 5/18/04
String substring  
Aug 1, 2004 7:21 PM



 
Hi,

I just want to ask how to make "c:\OS\example.java" to "c:\\OS
example.java.
Thank you..
any sample code?
 

Posts:316
Registered: 4/7/04
Re: String substring  
Aug 1, 2004 7:34 PM (reply 1 of 7)



 
I just want to ask how to make "c:\OS\example.java" to
"c:\\OS
example.java.
Thank you..
any sample code?

I'm not sure I understand your question. Are you talking about string manipulation. Lookup [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html]String[/url] in the API. Use String replace or replaceAll method
String s = "c:\\OS\\example.java";String s2 = s.replace("\\", "\\\\");
 

Posts:8,813
Registered: 10/4/00
Re: String substring  
Aug 1, 2004 7:34 PM (reply 2 of 7)



 
You are just going to love how intuitive this is
    String oldStr = "c:\\OS\\example.java";    System.out.println("oldStr="+oldStr);    String newStr = oldStr.replaceAll("\\\\","\\\\\\\\");    System.out.println("newStr="+newStr);
If you wonder why looking at a few of these might help
http://onesearch.sun.com/search/developers/index.jsp?and=backslash+replaceall+&nh=10&phr=regular+expressions&qt=&not=&field=&since=&col=devforums&rf=0&Search.x=33&Search.y=13
 

Posts:93
Registered: 5/18/04
Re: String substring  
Aug 1, 2004 9:44 PM (reply 3 of 7)



 
Hi,

my code is:

filePathArray = "c:\OS\Example.java"
then I want it to become: filePathArray = "c:\\OS
Example.java"

I try to put it like this but it said fatal error:

filePathArray = "c:\OS\Example.java";
fileName1 = filePathArray.replaceAll("\\","\\\\");

Can help me?
 

Posts:31,095
Registered: 4/30/99
Re: String substring  
Aug 1, 2004 10:00 PM (reply 4 of 7)



 
filePathArray = "c:\OS\Example.java";
is not valid Java. To put the backslash character in a Java string constant you have to "escape" it by putting another backslash before it. You have to do this instead:
filePathArray = "c:\\OS\\Example.java";
Note, this rule is only for string constants that you put in your Java code. It does NOT mean that every time you get a string, you have to do something to escape the backslashes. So if that was the reason for your question, just write your string constants correctly in your code and don't mess with the question any more.

PC²
 

Posts:54
Registered: 11/12/03
Re: String substring  
Aug 1, 2004 10:18 PM (reply 5 of 7)



 
Hi,

Just try this.., it will work.

c:/OS/Example.java

Best of luck,
R S

 

Posts:93
Registered: 5/18/04
Re: String substring  
Aug 1, 2004 10:48 PM (reply 6 of 7)



 
the filePathArray is actually I get it from the database.......
then I excpect it returns c:\os\example.java.
then want to change the '\' to '
'
How to do it?
 

Posts:316
Registered: 4/7/04
Re: String substring  
Aug 3, 2004 7:35 AM (reply 7 of 7)



 
the filePathArray is actually I get it from the
database.......
then I excpect it returns c:\os\example.java.
then want to change the '\' to '
'
How to do it?

Please read DrClap's response.

If you get c:\os\example.java from the database, why would you want to change it to c:\\os
example.java? If that is indeed what you want, see my earlier post. Backslash is an escape character for string literals.

filePathArray is also a misleading variable name since it is not an array of file paths.
 
This topic has 7 replies on 1 page.