Adding ''s To A String

[b][red]This message was edited by stricks at 2005-6-27 11:6:59[/red][/b][hr]
[b][red]This message was edited by stricks at 2005-6-27 6:24:7[/red][/b][hr]
I have a String that consists of, "C:My StuffCoolok.txt".

How can I first convert that into a string and then whenever there is a a '' character, add another one?

Actually, I can convert, I think, I just need help with the ''.

This is how I was trying to do it

[code]
public static String addSlash(String file){

char[] finshed_pathname, orginal_pathname;
int length = file.length();

orginal_pathname = new char[length];

file.getChars(0,length,orginal_pathname, 0);
System.out.println(length);

// Get teh length for the finishedFile array
for(int i = 0 ; i < orginal_pathname.length ; i++){
if(orginal_pathname[i] == '\'){
length++;
}
System.out.print(orginal_pathname[i]);
}
System.out.println();
System.out.println(length);


finshed_pathname = new char[length];

for(int i = 0, j=0 ; i < orginal_pathname.length ; i++){
finshed_pathname[j] = orginal_pathname[i];
System.out.print(finshed_pathname[j]);
if(orginal_pathname[i] == '\'){
j++;
finshed_pathname[j] = '\';
System.out.print(finshed_pathname[j]);
}

}
System.out.println();

file = new String(finshed_pathname);
return file;
}[/code]

Generates : \\ ...................................
BUT, the out.prints in the for loop creates this:
C:\Documents and Settings\stricbr3\Desktop\Info\Q&A.txt

What am I doing wrong?


Thanks




Comments

  • Hi

    You can't have in a String a '' because it is an
    illegal unicode escape, but you can have '/'.
    Try this :

    ...
    NewString = AddSlash("C:/My Stuff/Cool/ok.txt");
    //After NewString its = to "C://My Stuff//Cool//ok.txt"
    ...

    public static String AddSlash(String Str) {
    String AuxStr = "";
    for (int aa=0;aa<Str.length();aa++) {
    AuxStr = AuxStr + Str.charAt(aa);
    if (Str.charAt(aa) == '/')
    AuxStr =AuxStr + '/';
    }
    return AuxStr;
    }

    If this is what you were looking for please say something back.
  • No, not exactly. Thanks for the reply though.

    See, I need to read a windows file pathname and they're made up with (backslashes).

    But, I figured out another way to where I dont have to worry about doing it myself. Something about PropertyResourceBundles.

    Anyways thanks again for trying.

    Oh, but if anyone does know how to do it, feel free to tell me, I still wouldn't mind knowing just to know.
  • I am not sure how to use absolute paths to get to files. I am sure there is a way though.

    If I need to access a file in a different folder I use relative paths. Given your path "C:My StuffCoolok.txt", lets say that the class file is C:My Stuffprogramok.class". Use "../Cool/ok.txt" to get to it.

    This works for windows as well as linux and probably every other OS that has a Sun JVM. You can use backslashes like windows does and include escape characters for them ie "..\Cool\ok.txt", that works just fine with windows but is not portable, if that is important to you. My opinion in this case is make it portable, you don't lose anything by it.

  • : No, not exactly. Thanks for the reply though.
    :
    : See, I need to read a windows file pathname and they're made up with (backslashes).
    :
    : But, I figured out another way to where I dont have to worry about doing it myself. Something about PropertyResourceBundles.
    :
    : Anyways thanks again for trying.
    :
    : Oh, but if anyone does know how to do it, feel free to tell me, I still wouldn't mind knowing just to know.
    :

    I had to do the exact same thing in a program of mine. I'm due to submit the whole thing as part of a masters degree, so I'm not sure what the university policy is on publishing it on the internet, but here is the method I wrote for turning '' into '\' :

    -----------------------------------------------------------------------

    public String alterAddress(String inputString) {
    for (int index = 0; index < inputString.length(); index++) {
    if (inputString.charAt(index) == 'u005Cu005C') {
    String firstHalf = inputString.substring(0, index + 1);
    String secondHalf = inputString.substring(index);
    inputString = firstHalf+secondHalf;
    index++;
    }
    }
    return inputString;
    }

    -----------------------------------------------------------------------

    [note: the reason I have put 'index++' within the if-statement as well as it being in the for-loop is because the program will need to skip the next character once a '' has been encountered and doubled, otherwise a '' would spark off an infinite loop of '' characters]
  • Hi,

    Well I'm a little bit drunk right now.
    That's why I doubt to have understood your Problem.
    However, I think you work with Java "1.4.2" at least.
    Well if you want to replace all "" with "\" the following main method shows the trick.

    public static void main(String args[]) {
    String bla = "C:\hello\world";
    System.out.println(bla.replaceAll("\\", "\\\\"));
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories