Results 1 to 12 of 12
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default [SOLVED] problem with strings

    hi,
    i want to seperate float point number in my string,for example
    String str="ABCD23.50";
    i want float number 23.50 from above string .I tried with regex patterns ,but
    i didn't find a right solution .plaese help me .:o

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Iam Giving Completete Requirement Of My Program .
    I Have A String Like "abcd~ef~25.40..gh??"
    Now My Aim Is To Convert Above String ,in Such A Way That All Special
    Characters From The String ,except A Dot(.) Character That Exists
    Between Numbers.finally My Desired Strig Should Look Like
    "abcdef25.40gh".this Is My Problem.please Help Me It Is Urgent

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Don't capitalize each word, or say its urgent please. That message wasn't very clear, do you want to remove all special character except . ?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As I said earlier show that what you have done. In two posts you are explaining about two things.

  6. #6
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Thank u for your valuable suggestion and please excuse me for my mistakes .My requirement is to remove special characters from a string ,but whenever I find a 'dot' charcter between numbers it should not be affected.An example of my
    my requirement is
    String str="abcd~...$..123.456..efgh
    my output should look like this
    str=abcd123.456
    I tried with regex patterns ,but i am new to regex expressions, I am unable to understand difficult regex expressions.Please help me.
    Thank u.
    Last edited by sandeepsai39; 02-21-2009 at 11:43 AM.

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    String str="abcd~...$..123.456..efgh
    There are many dots in there.. whats special about the red one, and why shouldn't it also output the letters after?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can use regular expressions if you the know the exact pattern in general. Don't confused on it, exact not means the same thing, same order.

    Or else in simple way, you can check each character either special one or not you are looking.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    If I understood correctly.
    Java Code:
    public class SpecialCharacterRemover {
    
       public static void main(String[] args) {
          String input = "abcd~ef~25.40..gh??";
          String regex = "[^\\p{Alnum}.]|((?<!\\d)\\.)|(\\.(?!\\d))";
          String output = input.replaceAll(regex, "");
    
          System.out.println("==>" + output + "<==");
       }
    }
    There may be a simpler and/or more efficient regex that does the job.

    db

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No db, this is fine. But the thing is our thread starter can confuse on how to build that regular expression. Lets him to ask question here on your code, so later you can comment on that. :)

  11. #11
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    thank u darryl your code is working for me,but you missed one case for me.As eranga said i don't know even basics of regular expression.I know it is a strange requirement.It is better to tell the reason for this requirement.my module includes the removing the duplicate entries in the database.
    For example while entering the data manually,a person enter the string data "ABCD~12.50~",by mistake another person enter the same data like this
    "ABCD~.12.50~" ,these problems are quite common.For these two strings table generates two auto increamented primary keys.But while retriveving the data problems araises because we mostly depends on primary key.For such cases i need to remove duplicate entries.First i used the simple regex pattern like this [^a-zA-Z0-9] to remove the special cahracters from the two strings and i compared two for duplicates ,but in later cases i faces the problems like "ABCD~25.30~" and "ABCD~2530~" ,these two are different and may not be same ,because 25.30 and 2530 may not be a data entry problem ,for this situtations i used very bad algoritham like this
    1) finding 'dot' character in evering string
    2) checking if '.' characters exists between two numbers ,based on this condition ,i performed
    actions what i required.
    But it is very bad way to write like this ,it consumes time,because of this reason iam not happy to show my code.
    This reason pushed me to use "regex patterns".
    I hope all of you understand my requirement,i feel very happy if you tell mistakes what i had done or if u tell a better solution.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you are not much keen on regular expressions, my opinion is work on in basis way. As I said earlier go through the each character of the string and search for the special character you want to remove. Manipulate the new string avoiding those.

Similar Threads

  1. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  2. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  3. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM
  4. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 09:10 PM
  5. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •