This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

backslashes need to be replaced

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to replaces backslashes from a string.
example.
the string
"the\man\is\45\years\of\age"
must be changed to
"the/man/is/45/years/of/age"
substring,substr,charAt ... etc are not working.
How to do it.
The string is a file path coming from an xml file.
Thanks
Ashu.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you have is that javascript interprets the backslash character as a special character. If you want to create a string containing one, you actually have to use two. For example, a valid file path would look like this:

If your path only contains one slash, that slash and the next character following it will be interpreted as a special character. For example, \t would be a tab, \n would be a newline, etc.
So the question is this: do you have any control over the creation of the XML string? If not, I think you may be hosed. If so, create your file path using \\ instead of \.
 
Ashutosh Uprety
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bodie,
if it was a "\\" i wouldn't have wanted to bother any of u guys. But thats the problem.
the string can come from anywhere i.e. xmlfile or array.
i thought of another way, but dont seem to be getting the right method method for it.
It goes like this.
In BASIC, there is a function right() which counts the index from the right side.
If there is any such method in javascript/jscript then we could use the following logic. Below I will call this method "rightIndexOf()" for my sake
myString = "a/bc/def/34/ghi"
stringLength = myString.length
for(i=0; i < stringLength; i++)
if(myString.indexOf(i) == myString.rightIndexOf(stringLength - (i+1)))
{
newString += myString.charAt(i);
}
else
{
it means that the character at indexOf(i) is '\';
newString += '/'
}
}
The problem is that I am not able to find any method in javascript/jscript which gives this "rightIndexOf()" type of functionality.
If anyone has any ideas, pls do let me know
Thanks
Ashu.
 
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this script. Hope it helps you
s = "This\\is\\a\\test\\string";
alert(s);
y = "";
for(x=0;x<s.length;x++){
if(s.charAt(x) == "\\"){
y += '/'
} else {
y += s.charAt(x);
}
}
alert(y);
 
Vijay S. Rathore
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for posting it so early. Wait I am still working on it.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem that I ran into is that Javascript performs the translation from the escape sequence to the character that it represents. So, for example, \t translates to a tab. Thus, the length of the string c:\test is only 6, not 7 as you might expect. This messes up any attempt at using charAt(), indexOf() or similar string manipulation functions. So far, I haven't found a good way around this problem.
Interestingly, HTML elements know enough to handle this correctly. I tried getting the string c:\test from an INPUT element and found it to be using \\ in place of \.
Sorry I couldn't help.
 
Ashutosh Uprety
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes bodie,
thats exactly what happens.
c:\test is of length 6 and not 7.
it seems that nothing can be done if the string has been coded wrongly. till now i haven't been able to figure out a way to deal with this.
would welcome more brains to enter into this unseemingly tricky problem.

Ashu
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you can play with this line
TheStr=TheStr.split("X").join("Y");
where TheStr is the string that you have
X would be the character(s) you would want to replace
Y would be the character(s) that would replace the X
Hope you understand what I was trying to say.
 
This is my favorite show. And this is my favorite tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic