Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Java] string.replaceAll() isn't working....

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu May 03, 2012 10:06 am    Post subject: [Java] string.replaceAll() isn't working.... Reply with quote

Code:
       String s = " 4 . 5 3 2 ";
      s = s.replaceAll("\\s+","");
      //s = s.replaceAll(" ", ""); //alternative
      System.out.println(s);

Prints 4.532

Yet
Code:

          s[i] = s[i].replaceAll("\\s+","");
          s[i] = s[i].replaceAll(" ", ""); // alternative doesnt work either
          System.out.println(s[i]);
          


where S[i] = " 4 . 5 3 2 ". it still prints out 4 . 5 3 2 , not 4.532 like it should...

Any ideas why? My sanity is on the line here Crying or Very sad

_________________
Back to top
View user's profile Send private message
661089799107
Expert Cheater
Reputation: 3

Joined: 25 Jan 2009
Posts: 186

PostPosted: Thu May 03, 2012 12:00 pm    Post subject: Reply with quote

Works fine for me.

Code:

String[] s = new String[] {" 4 . 5 3 2 "};
      
s[0] = s[0].replaceAll("\\s+","");
       
System.out.println(s[0]);


Output: 4.532
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu May 03, 2012 1:00 pm    Post subject: Reply with quote

String [] s = { "4 . 5 6", "2. 6 8", "3 . 6 3", .....} and so on which I got by .split() ing a larger string into those individual strings).
Then I'm passing s into a function:

public static double myFuncccction(String[] s, int low, int high){

for(int i = low; i<high; i++){
System.out.println(s[i]);
g[i] = s[i].trim(); //optional
g[i] = s[i].replaceAll("\\s+","");
g[i] = s[i].replaceAll(" ", "");
System.out.println(s[i]);

}

I just want the strings to be "4.56" and so on, without white spaces, so I can convert them to a double for other uses.


However it prints out the same before and after the call to .replaceAll()

_________________
Back to top
View user's profile Send private message
661089799107
Expert Cheater
Reputation: 3

Joined: 25 Jan 2009
Posts: 186

PostPosted: Thu May 03, 2012 2:13 pm    Post subject: Reply with quote

manc wrote:
String [] s = { "4 . 5 6", "2. 6 8", "3 . 6 3", .....} and so on which I got by .split() ing a larger string into those individual strings).
Then I'm passing s into a function:

public static double myFuncccction(String[] s, int low, int high){

for(int i = low; i<high; i++){
System.out.println(s[i]);
g[i] = s[i].trim(); //optional
g[i] = s[i].replaceAll("\\s+","");
g[i] = s[i].replaceAll(" ", "");
System.out.println(s[i]);

}

I just want the strings to be "4.56" and so on, without white spaces, so I can convert them to a double for other uses.


However it prints out the same before and after the call to .replaceAll()


What is 'g'? You are outputting 's'.
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu May 03, 2012 2:19 pm    Post subject: Reply with quote

Blacknight wrote:
manc wrote:
String [] s = { "4 . 5 6", "2. 6 8", "3 . 6 3", .....} and so on which I got by .split() ing a larger string into those individual strings).
Then I'm passing s into a function:

public static double myFuncccction(String[] s, int low, int high){

for(int i = low; i<high; i++){
System.out.println(s[i]);
g[i] = s[i].trim(); //optional
g[i] = s[i].replaceAll("\\s+","");
g[i] = s[i].replaceAll(" ", "");
System.out.println(s[i]);

}

I just want the strings to be "4.56" and so on, without white spaces, so I can convert them to a double for other uses.


However it prints out the same before and after the call to .replaceAll()


What is 'g'? You are outputting 's'.


just an accident, they should be s's

_________________
Back to top
View user's profile Send private message
661089799107
Expert Cheater
Reputation: 3

Joined: 25 Jan 2009
Posts: 186

PostPosted: Thu May 03, 2012 2:53 pm    Post subject: Reply with quote

Again it works perfectly fine.

Code:

   public static double myFuncccction(String[] s, int low, int high) {
      for(int i = low; i < high; i++) {
         System.out.println(s[i]);
         s[i] = s[i].trim(); //optional
         s[i] = s[i].replaceAll("\\s+","");
         s[i] = s[i].replaceAll(" ", "");
         System.out.println(s[i]);
      }
      
      return 0.0d;
   }


Code:

      String [] s = { "4 . 5 6", "2. 6 8", "3 . 6 3"};
      
      myFuncccction(s, 0, s.length);


Output:

Code:

4 . 5 6
4.56
2. 6 8
2.68
3 . 6 3
3.63
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites