View RSS Feed

Java String

All about Java String.

  1. Java String comparison with the compareTo method

    by , 04-26-2012 at 08:02 PM
    Also a less common 3rd way for comparison of the Java Strings is present. That method is CompareTo method, with string class. In case of two similar Java Strings, this method will be returning a zero value. String Comparison approach shall look like this given example:

    Java Code:
    String string1 = "foo bar";
    String string2 = "foo bar";
    
    // java string compare example
    if (string1.compareTo(string2) == 0)
    {
      // this line WILL
    ...
    Categories
    Compare
  2. equalsIgnoreCase method

    by , 04-26-2012 at 08:01 PM
    It shall be ignore in Java string comparison tests that whether strings are uppercase of lowercase. Use EqualsIgnoreCase mthods for equality so that to test the strings .
    Java Code:
    String string1 = "foo";
    String string2 = "FOO";
    
    // java string compare while ignoring case
    if (string1.equalsIgnoreCase(string2))
    {
      // this line WILL print
      System.out.println("Ignoring case, the two strings are the same.")
    ...
    Categories
    Compare
  3. Java String comparison with the equals method

    by , 04-26-2012 at 08:00 PM
    About 95% of time, I prefer comparing strings by using Java String class’ equal methods. It is shown below:
    Java Code:
    if (string1.equals(string2))
    String equal method observes if exactly similar string of characters are present in Java strings, they would be considered as equal.
    Quick String comparison could be observed, with equal method, 2 strings wouldn’t be equal as same characters are not present.
    Java Code:
    String string1 = "foo";
    String string2
    ...
    Categories
    Compare
  4. Java String compare to determine Equality

    by , 04-26-2012 at 07:59 PM
    Comparison of the java string could be done in various ways, as given below. It is dependent upon javastringcompare type that which of them will be used.
    * == Operator
    * equals method
    * compareTo method
    Comparing, by using the “== Operator”
    When String object references are compared, ==operator is used. If 2 variables of String are pointed towards similar object present in memory, then comparison will be returned true. In other case it will return false. It shall ...
    Categories
    Compare
  5. Compare strings

    by , 04-26-2012 at 07:58 PM
    Two strings comparison will be learnt in this section. Java lang package gives methods for comparison of 2 with either lower or upper case. Facility is being provided by the equals() method to compare 2 strings. Equal() methods are used by the given program to help you for comparing 2 strings. In case when both of the strings present are equal then a message “Given strings equal” would be displayed. Otherwise, “given strings unequal” would be shown.
    Description of code:
    equals(): ...
    Categories
    Compare
  6. Convert String to Number

    by , 04-26-2012 at 07:57 PM
    This section shows how Number is converted into String. Functionaliy is provided by this program for conversion of String into Number,
    Code Description:
    In this program, String os Boolean, int, byte, double is being taken as it is given below. Also, MIN_VALUE and MIN_VALUE methods are used which are different wrapper classes so that all values on double, short and int could be printed.
    Here is the code used:
    Java Code:
    public class StrToNum{
      public static void main
    ...
  7. Convert Number to String

    by , 04-26-2012 at 07:56 PM
    This sections shows how numbers can be converted into String. Given program gives the functionality for conversion of numbers to String.

    Code Description:
    Different kind of numbers have been declared, for example, long, int and float etc given below. valueOf() method is used which is basically Stringclass’s method. For numeric conversions String.valueOf() is used & numeric classes are converted into String by using toString() method, as given below:
    Program’s code ...
  8. Regular Expressions, Literal Strings and Backslashes

    by , 04-26-2012 at 07:55 PM
    In literal strings, backlash is considered as escape character. It is basically a single backlash i.e. “\\”. Backlash is also considered an escape character in case of regular expressions. A single backlash is matched to the regular expression \\. As a java string, regular expression would become “\\\\”. Four backlashes are used for matching just one single.

    Word character is being matched with regex \w. It is abbreviated as "\\w".

    Similar backlash mess up ...
  9. Java Matcher Class

    by , 04-26-2012 at 07:54 PM
    Matcher class instances match the character sequences, against string pattern. Matchers are provided with input by CharSequence so that to provide support to matching, against characters from input sources.

    Pattern helps to create the matcher when pattern’s matcher method is invoked. Once it has been made, different match operations could be performed by matcher.
    • Whole input sequence is matched by the matcher method against the pattern.
    • Input sequence is matched by the
    ...
    Categories
    Regex
  10. Java Pattern Class

    by , 04-26-2012 at 07:54 PM
    Pattern class instance presents the regular expression which has been specified in string form, in syntax, same as used by Perl.

    Regular expression shall be compiling first to the Pattern class instance. Matcher object is created by ther resulting pattern, which matches with the sequence of the arbitrary character. Similar patterns are shared by various matchers as it is stateless.

    Given regular expression is compiled by the compile method into a pattern. After this, ...
    Categories
    Regex
  11. Pattern Method Equivalents in java.lang.String

    by , 04-26-2012 at 07:53 PM
    Support of the regular expression is also present in java.lang.String via different methods which mimic the java.util.regex.Pattern behavior. From APIs, their key excerpts are shown below for convenience.

    Public Boolean matches (String regex):
    Show whether string will be matching with the regular expression or not. This method’s invocation of the form str.matches produces similar output that was produced by expression Pattern.matches.

    Public string[] split (String ...
    Categories
    Regex
  12. Java String Boundary Matchers

    by , 04-26-2012 at 07:51 PM
    Till now we were just interested in this thing that if match is present at a location in some specified string. Care hs never been taken regarding the location of match in the string, where it actually took place.

    Pattern matches could be made much more precise just by making information specific along with boundary matches. Let us say, e.g, may be one of you are more keen to find out a specific word but only when it is present at start or end of line. May be you desire to be aware ...
    Categories
    Regex
  13. Differences Among Greedy, Reluctant, and Possessive Quantifiers

    by , 04-26-2012 at 07:50 PM
    Subtle difference is present b/w reluctant, greedy and possessive quantifiers.

    Greedy ones are known as greedy as they enforce matchers to eat or read in the whole input string before the 1st match attempt. When 1st attempt of match gets failed, input string is back offed by the matcher by 1 character and then tries once again in which the process is repeated till the match is being found or no more characters have been left for backing off. It totally depends upon quantifier that ...
  14. Java String Quantifiers

    by , 04-26-2012 at 07:49 PM
    Number of occurrences are specified by the quantifiers so that to match. For sake of convenience, Pattern API has been divided into 3 sections with specifications that describe possessive, greedy and reluctant quantifiers are represented below. In first look, this might happen that X?+, X? And X?? Be doing similar things as all of them promise to be match X i.e once, or not at all. Subtle differences of implementations are present that will be discussed at the end of this section.

    ...
    Categories
    Regex
  15. What are Metacharacters

    by , 04-26-2012 at 07:47 PM
    Special characters are also supported by this API which places an affect at the mode by which a pattern is matched. Regular expressions can be changed to cat. & input string to tocats. Output will be as following:
    Enter regex i.e. cat.

    Enter “input string” for searching i.e. cats
    Text cats are found that start at index zero & end at index four.
    Match succeeds even dot is absent in input string. Match succeeds as dots are metacharacters, a character that ...
    Categories
    Regex
  16. String Literals

    by , 04-26-2012 at 07:46 PM
    Basic pattern matching form that is being supported by API is string literal match. E.g, when regular expression and input string is foo, matching succeeds as strings are same. Test harness could be used to try it.

    Enter regex: foo
    Enter “input string” for search: foo
    Text foo is found that starts at index 0, & ends at index 3.

    Match was successful. It shall be noted that when input strings are three characters longer then starting index is zero and ...
    Categories
    Regex
  17. How Are Regular Expressions Represented?

    by , 04-26-2012 at 07:45 PM
    Package java.util.regex has three classes: PatternSyntaxException, Matcher and Pattern.

    • Pattern object is basically the compiled presentation of regular expression. For pattern creation first thing is invoking public static methods of compilation that will make pattern object to be returned. Regular expression is accepted by these methods being 1st argument; 1st trial lessons will let you know regarding the required syntax.
    • Matcher object is considered to be an engine which will
    ...
    Categories
    Regex
  18. Using Regular Expressions with String.matches()

    by , 04-26-2012 at 07:44 PM
    Built in support is provided by Strings present in Java for the regular expressions. There are 3 built in methods present in Strings for regular expressions. For example: replace(), split() and matches().

    There is no optimization of methods for performance. Classes will be used later on, which are performance optimized.

    s.matches("regex"): Evaluates if "regex" matches s. Returns only true if the WHOLE string can be matched
    s.split("regex") ...
    Categories
    StringBuffer
  19. Regular Expressions Overview

    by , 04-26-2012 at 07:42 PM
    For strings, search pattern is defined by the regular expression. Such kind of pattern might be matching one time or several times, or not at all. Regular expression has an abbreviation “regex”.
    String (literal) is a simpler example of regular expression. E.g. Hello World string will be matched with Hello World regex.

    Other regular expression example is (dot) “.” That match with just one single character; and it will be matching e.g. “z”or ”a” or ”1”.

    Usage ...
  20. Memory usage of StringBuilder and StringBuffer

    by , 04-26-2012 at 07:39 PM
    The implementation of these classes is essentially done in similar manner through the shared subclasses & usage of their memory is also same. Difference comes when the turn of those methods come that are present at StringBuffer get synchronized. Here they will be referred as the string buffers generically for sake of simplicity however; comment at the usage of memory will be applied to both classes.

    Object’s overhead is required by the string buffer, space for sake of int field ...
  21. StringBuffer Functions

    by , 04-26-2012 at 07:38 PM
    Few StringBuffer methods has many uses which is being explained by the following program:

    1. capacity()
    String buffer’s current capacity is returned.

    2. length()
    String buffer’s length is returned.

    3. charAt(int index)
    Specified sequence character which is presented by string buffer are returned.

    4. setCharAt(int index, char ch)
    At specified index, the character of this string buffer has been set to ch
    ...
  22. StringBuffer

    by , 04-26-2012 at 07:36 PM
    Thread safed & mutable character sequence. Basically it is just like String however modifications could be done with it. At any time, few specified character sequences are present in it but the content or length of that sequence might be changed via method calls.

    Mutilple threads may use string buffers safely. Synchronization of methods is done where it is considered to be an important thing hence all operations that take place at some specific instance would behave like they ...
    Tags: stringbuffer Add / Edit Tags
    Categories
    StringBuffer
  23. Understanding String memory usage

    by , 04-26-2012 at 07:36 PM
    Fields which are present at the String object shall be looked first of all so that for a better understanding of the above calculations:

    • a char array: Separate object which consist of the actual characters;
    • an integer offset where the string starts;
    • String Length
    • Another int, for hash code’s cached calculations.



    This shows that if no characters are present in a string, even then 4 bytes would be required for sake of the char array reference along ...
    Categories
    String
  24. Memory usage of Java Strings and string-related objects

    by , 04-26-2012 at 07:35 PM
    Everywhere strings are present. It is quite difficult thing to write down an application that is not used by them or others use it extensively. Various typical applications like server entities that are obtained from databases, or data present to be displayed at web, most of the objects that have been stored on heap might be Strings. Therefore, when usage of memory is crucial it means that understanding of the Strings memory usage or the related objects is also crucial.

    If your have ...
    Categories
    String
  25. Getting Characters and Substrings by Index

    by , 04-26-2012 at 07:34 PM
    Character can be obtained at one particular index, within a string, by charAt() accessor method’s invoking. First character index is 0 however last character index is length()-1. This given example shows the code which obtains the character, at index 9 present in string.
    String anotherPalindrome = "Niagara. O roar again!";

    char aChar = anotherPalindrome.charAt(9);

    Indices start with 0 therefore the characters which are present at the index 9 are 'O'. ...
    Categories
    String
  26. Converting Strings to Numbers

    by , 04-26-2012 at 07:32 PM
    In a string object, usually a program would be ending up with numeric data, a value which user will enter. For example:
    Primitive numeric types (Short, Integer, Byte, Double, Long and Float) are being wrapped by the Number subclasses. A class method known as valueOf is provided by each which makes a string to be converted into object of that type. For example, ValueOfDemo will be getting 2 strings to convert them into no. and then performing the arithmetic operations at values.
    ...
    Categories
    String
  27. The difference between == and equals()

    by , 04-26-2012 at 07:31 PM
    The == operator:
    References are compared by == operator, of the 2 objects which are present in memory. When these 2 objects are pointing similar memory location, then it returns true. It shall be remembered that in java strings are immutable therefore if string variables “str1” is present along with “abc” value and then another variable along with other value is created, instead of new string variable creation with same value, then Java simply would be pointing str2 to similar location of ...
    Tags: equals() Add / Edit Tags
    Categories
    String
  28. String Conversion and toString() in Java

    by , 04-26-2012 at 07:30 PM
    When data is converted into string representation while concatenation is done, versions that are overloaded string conversion methods valueOf() are called. They are defined by the help of String.valueOf(), which are overloaded for type Object and all simpler types. In case of simple types, a string is returned by the valueOf() in which human readable are present that are equal to value that has been called. On the object, toString() method is called by ValueOf(). Here toString() method shall be ...
    Categories
    String
  29. String Equality and Interning

    by , 04-26-2012 at 07:29 PM
    In Java Strings are the objects however they resemble primitives like charS or intS. In that java source, code may be having the String literals and + operator might be used to concatenate the Strings. These features are quite convenient, however similarity present b/w Strings & primitives may cause certain confusions, when comparison is done of the Strings.

    Two mechanisms are provided by Java to do the test for equality. Use “==” operator for testing of the primitive values equality. ...
    Categories
    String
  30. Differences between String and StringBuffer in Java

    by , 04-26-2012 at 07:29 PM
    Major difference that exist b/w StringBuffer and String is the immutable string, while mutability of the StringBuffer means that StringBuffer object could be modified once it has been created, without creation of new object. StringBuffer is made an ideal choice by this mutable property to deal with Java strings. StringBuffer can be converted into String by the help of toString() method. StrinBuffer Vs String, or difference b/w String & StringBuffer is most popular for the interview questions, ...
    Categories
    String
Page 1 of 2 12 LastLast