• Intro Headers Instructions Macros Commands
Fork me on GitHub
    • Getting Started
      • Introduction
      • How to install bnd
      • Guided Tour Workspace & Projects
      • bnd / bndlib Features and JAR Wrapping Techniques
      • FAQ - Frequently Asked Questions
    • Concepts and Practices
      • Concepts
      • Background
      • Best practices
    • Build and Development
      • Build
      • Generating JARs
      • Versioning
      • Baselining
      • Service Components
      • Metatype
      • Contracts
      • Bundle Annotations
      • Accessor Properties
      • SPI Annotations
    • Dependency and Launching
      • Resolving Dependencies
      • Launching
      • Startlevels
    • Testing
      • Testing
      • Testing with Launchpad
    • Packaging and Distribution
      • Packaging Applications
      • JPMS Libraries
      • Wrapping Libraries to OSGi Bundles
    • Documentation and Tools
      • Generating Documentation
      • bnd CLI Commands
      • For Developers
      • Templates for Workspaces
      • Tips for Windows users
      • Tools bound to bnd
    • Reference Material
      • Reference
      • Headers
      • Instruction Reference
      • Instruction Index
      • Macro Reference
      • Macro Index
      • Plugins
      • External Plugins
    • Configuration and Troubleshooting
      • Settings
      • Errors
      • Warnings
  • compare STRING STRING

    Prev Next
    Macro
    Compare two strings by using the compareTo method of the String class.

    Summary

    Compare two strings lexicographically using Java’s String.compareTo() method, returning -1, 0, or 1.

    Syntax

    ${compare;<string1>;<string2>}
    

    Parameters

    • string1: First string to compare
    • string2: Second string to compare

    Behavior

    Compares the two strings lexicographically (alphabetically) and returns:

    • 0 - The strings are equal
    • 1 - The first string is greater than the second (comes later alphabetically)
    • -1 - The first string is less than the second (comes earlier alphabetically)

    The comparison is case-sensitive and uses Unicode character values.

    Examples

    # Equal strings
    ${compare;hello;hello}
    # Returns: 0
    
    # First string greater
    ${compare;world;hello}
    # Returns: 1
    
    # First string less
    ${compare;apple;banana}
    # Returns: -1
    
    # Case-sensitive comparison
    ${compare;Apple;apple}
    # Returns: -1 (uppercase comes before lowercase)
    
    # Use in conditional logic
    -include ${if;${compare;${env;MODE};production};prod.bnd;dev.bnd}
    
    # Numeric strings (lexicographic, not numeric)
    ${compare;10;2}
    # Returns: -1 (because "1" < "2" alphabetically)
    
    # Empty string comparison
    ${compare;;text}
    # Returns: -1 (empty string is less than any non-empty string)
    

    Use Cases

    1. Conditional Logic: Make decisions based on string comparison
    2. Sorting Logic: Implement custom sort orders in build configurations
    3. Version Comparison: Compare simple version strings (use vcompare for OSGi versions)
    4. Configuration Selection: Choose configuration based on string values
    5. Validation: Check if strings match expected values

    Notes

    • Comparison is lexicographic (alphabetical), not numeric
    • For numeric comparison, use ncompare
    • For OSGi version comparison, use vcompare
    • Comparison is case-sensitive
    • Returns only -1, 0, or 1 (not the actual difference between strings)
    • Useful with if macro for conditional builds

    Related Macros

    • ncompare - Compare numbers numerically
    • vcompare - Compare OSGi version strings
    • if - Conditional macro that can use comparison results

    See test cases in MacroTestsForDocsExamples.java

Prev Next
Search
    • Home