Results 1 to 18 of 18
Like Tree3Likes
  • 1 Post By Tolls
  • 1 Post By Norm
  • 1 Post By Tolls

Thread: String methods

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default String methods

    Is there any website or pdf file, where it contains all String methods ( such as usbstring, stringbuffer, usedDlimiter...) with code examples?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String methods

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: String methods

    There are probably examples scattered around the web but I don't know of any one document or site that has them all.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: String methods

    There comes question. Is it possible to call double parameters for String method?

    Theres an example:

    Java Code:
    while ( sn.hasNext () )
        {
           String om = sn.nextLine ();
           String [] sentence = om.split("\\d");
           String [] end = om.split (":");
           
           System.out.println ( sentence[0] + end [1]  );  
        }
    As you can see, this codes does 2 splittings for every line.
    To do this, it requires to to use parameters twice.

    Because I cant write like this:
    Java Code:
    String [] sentence = om.split("\\d" && ":");

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: String methods

    Yes. You can also do it another way.

    Java Code:
    public class SplitDemo {
       public static void main(String[] args) {
          
          String om = "word2test:another";
          String[] sentence = om.split("\\d");
          String[] end = om.split(":");
          System.out.println(sentence[0] + end[1]);
          
          // You can also do it like this
          
          String[] words = om.split("[\\d:]"); // split on a digit or a :
          System.out.println(words[0] + words[2]);
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String methods

    The arg to the split() method is a regular expression. Regular expressions can be very complicated. I do not know if it is possible to write a regular expression for what you want to do.

    I would use String class methods to find where the dividing points in the input String are and then use the substring method to access the desired part.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: String methods

    Quote Originally Posted by asdfg View Post
    Is there any website or pdf file, where it contains all String methods ( such as usbstring, stringbuffer, usedDlimiter...) with code examples?
    Have you tried this link: https://docs.oracle.com/javase/8/docs/api/
    It has the documentation of all methods of all classes and then some ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: String methods

    Quote Originally Posted by Norm View Post
    Regular expressions can be very complicated.
    What's that quote?
    Something like "if the solution to your problem is a regular expression then you've just created two problems".
    ;)

    (Note, they have their place, but sometimes people get carried away with them)
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: String methods

    Where I can get list of all regex symbols such as \\D \\w. Because they often can be combined to string methods.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String methods

    Look at the API doc for the Pattern class.
    asdfg likes this.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: String methods

    I have a text:
    WallWindowDoor
    I want to seperate those 3 words.
    Java Code:
    String[] result1 = p.split (p.toUpperCase() );
    This dont works.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: String methods

    Do you know the text before hand, or do you just want to find all words, which will always start with a capital?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: String methods

    You have to have delimiter space in between words to get separated...
    Ramya:cool:

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: String methods

    A quick Google (using the terms 'java string split on capitals') gives me this SO thread as the first result:
    regex - Java: Split string when an uppercase letter is found - Stack Overflow

    In there is a regex that works for what you need, but read the marked answer first so you understand what is going on...the bit you want extends from that correct answer (as your requirements are subtly different).
    asdfg likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: String methods

    example: AaaBbbCccDdd
    result: Aaa Bbb Ccc Ddd

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: String methods

    Yes you need a delimiter but that doesn't mean you can't split like the OP wants. Think zero-width-positive lookahead assertions.
    Java Code:
    String [] w = words.split("(?=[A-Z])");
    Before I got hooked on Java I programmed extensively in Perl which also has these. Unfortunately, they aren't really explained all that well in the Java API. But they can be very useful.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: String methods

    Those REs are heavily overused and abused; they can cause very inefficient runtime behaviour (that big-oh thing) when you make those REs backtrack; i.e. the RE a*a{10}b (at least 10 'a's followed by a 'b') backtracks 10 times given an input string such as "aaaaaaaaaab"; almost everyone can see that the same can be done much more efficiently (by not using REs) ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: String methods

    On the other hand, they can be useful. Here is my (very) contrived example to prevent needless backtracking.

    The following takes a noticeable amount of time to fail when matching s against pat.
    Java Code:
    String pat = "a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*ab";
    String s = "aaaaaaaaaaaaaaaac";
    An improved expression using independent grouping can allow a fast fail by preventing backtracking
    Java Code:
          String pat = "(?>(a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a))b";
          String s = "aaaaaaaaaaaaaaaac";
    In both cases, a successful match is immediate.

    Like anything else in programming, use what's available and if it doesn't work or causes
    problems, then try something else.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. If Statement Modification (Using String Methods)
    By danthegreat in forum New To Java
    Replies: 29
    Last Post: 09-11-2011, 04:51 PM
  2. Accessing int's, String's by methods.
    By Cyloc in forum New To Java
    Replies: 2
    Last Post: 12-14-2010, 02:25 AM
  3. calling different methods based on a string?
    By Cruncher in forum Advanced Java
    Replies: 3
    Last Post: 10-05-2010, 01:25 PM
  4. How to keep a string through out diffrent methods
    By bmist in forum Java Applets
    Replies: 33
    Last Post: 05-27-2010, 07:24 PM
  5. Problems implementing Java string methods
    By everlast88az in forum New To Java
    Replies: 8
    Last Post: 11-06-2008, 05:17 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
  •