User Tools

Site Tools


programming:java:java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:java:java [2023/11/02 13:49] – [Regular Expressions (java.util.regex)] skipidarprogramming:java:java [2023/11/02 14:33] (current) – [Records] skipidar
Line 1115: Line 1115:
  
 When using Regex - make sure you dont introduce too greedy patterns When using Regex - make sure you dont introduce too greedy patterns
 +
 +https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
 +<code>
 +Boundary matchers
 +^ The beginning of a line
 +$ The end of a line
 +\b A word boundary
 +\B A non-word boundary
 +\A The beginning of the input
 +\G The end of the previous match
 +\Z The end of the input but for the final terminator, if any
 +\z The end of the input
 +</code>
 +
 +
 +==== Tokenize strings ====
 +
 +Break into single words. 
 +
 +<sxh java>
 +
 +        String[] result = "this is a test".split("\\s");
 +        for (int x=0; x<result.length; x++)
 +            System.out.println(result[x]);
 +
 +/*      this
 +        is
 +        a
 +        test
 +*/
 +
 +</sxh>
 +
  
 <sxh java> <sxh java>
Line 1137: Line 1170:
 </sxh> </sxh>
  
 +==== ReplaceAll  ====
  
 +<sxh java>
 +    @Test
 +    void stringReplaceAll() {
 +        String s = "bob and alice like to text each other";
 +        String john = s.replaceAll("\\b(bob)\\b", "john");
 +        System.out.println(john);
 +    }
 +
 +</sxh>
 +
 +
 +===== Records =====
 +First: Java 14
 +
 +
 +
 +<sxh java>
 +    // some interface
 +    interface MyBarkInterface{
 +        default void bark(){
 +            System.out.println("bark bark");
 +        }
 +    }
 +
 +    record MyPoint(int x, int y) implements MyBarkInterface{};
 +
 +    // Cant inherit from Record - its final
 +    //record MyPoint2(int x, int y) extends MyPoint{};
 +
 +
 +    @Test
 +    void recordTest1() {
 +        MyPoint p = new MyPoint(1,2);
 +
 +        System.out.println(p); // MyPoint[x=1, y=2]
 +        System.out.println(p.x()); // can access vars via methods
 +        System.out.println(p.x()); // can access vars via methods
 +        
 +        p.bark(); // can access hiearchy
 +    }
 +
 +</sxh>
programming/java/java.1698932942.txt.gz · Last modified: by skipidar