How to check empty NSString

Checking empty string

NSString *string1=@"";
NSString *string2=@" ";
NSString *string3=@"  \n ";



string1 is an empty string.

string2 is not empty having empty whitespace
[[string2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

string3 is not empty having empty whitespace and new line character
[string3 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

all the above string can be checked with @"" to test empty string.

how to create singleton object

Singleton object are created only once in the life cycle of the app.

Singleton object can be accessed using [ClassName sharedInstance].

+(id)sharedInstance{
    static MyClassManager *sharedMyClassManager = nil;
    static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
          sharedMyClassManager = [[self alloc] init];
     });
     retrun sharedMyClassManager;
}

WebRep
currentVote
noRating
noWeight

@property types

Objects have properties to access for public.
Types of attributes

atomic - can be accessed only by one thread at a time.
non-atomic - can be accessed by any number of threads.
readonly - having access for readonly for object.
readwrite - having access for read and write object.
retain - will increase the retain count of the object. This is called as shallow copy. This will not create new memory reference.

copy - will create new memory location and copy all data to the newly created object. This is called as Deep copy.
assign - This will neither create new reference nor increase the retain count of the object. All delegate properties are assign.


How to install xcode


Download Xcode from https://developer.apple.com/xcode/ 
Drag and drop to applications.
Then open Xcode from Applications.

StringArray to String in java


  • By Using StringUtils.join method in apache common we can easily convert String[] to String.
  • You can download this jar from Here.


  1. package commonsUtils;
  2. import org.apache.commons.lang.StringUtils;

  3. public class SampleRunnable {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. String[] stringArray = { "one", "two", "three", "four", "five" };
  7. String finalString = StringUtils.join(stringArray,","); //Output comma(,) seperated string
  8. String finalString2 = StringUtils.join(stringArray,":"); //Output colon(,) seperated string
  9. System.out.println(" finalString :: "+finalString);
  10. System.out.println(" finalString2 :: "+finalString2);
  11. }
  12. }
 Output :


 finalString :: one,two,three,four,five
 finalString2 :: one:two:three:four:five



Format Date in java

  • DateFormatUtils.format Utility method is used to format date from date to string.
  • You can use this method with the help of commons-lang-2.6.jar.
  • You can download this jar from Here.

  1. package commonsUtils;
  2. import java.util.Date;

  3. import org.apache.commons.lang.time.DateFormatUtils;

  4. public class SampleRunnable {

  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. Date today = new Date();
  8. String firstPattern = DateFormatUtils.format(today,"MM/dd/yyyy HH:mm:ss");
  9. System.out.println("Date to String in MM/dd/yyyy HH:mm:ss::"+firstPattern);
  10. String secondPattern = DateFormatUtils.format(today,"dd/MM/yyyy HH:mm:ss");
  11. System.out.println("Date to String in dd/MM/yyyy HH:mm:ss::"+secondPattern);
  12. String thirdPattern = DateFormatUtils.format(today,"MM/dd/yyyy");
  13. System.out.println("Date to String in MM/dd/yyyy::"+thirdPattern);
  14. String fourthPattern = DateFormatUtils.format(today,"dd/MM/yyyy");
  15. System.out.println("Date to String in dd/MM/yyyy::"+fourthPattern);
  16. }

  17. }


Output :

 Date to String in MM/dd/yyyy HH:mm:ss::07/17/2013 15:06:30
 Date to String in dd/MM/yyyy HH:mm:ss::17/07/2013 15:06:30
 Date to String in MM/dd/yyyy::07/17/2013
 Date to String in dd/MM/yyyy::17/07/2013



File.separator vs File.pathSeparator



  • File.separator == > separator: Platform dependent default name-separator character as String. For           windows, it’s ‘\’ and for unix it’s ‘/’.
  • File.separatorChar ==>separatorChar: Same as separator but it’s char.
  • File.pathSeparator ==> pathSeparator: Platform dependent variable for path-separator. For example PATH or CLASSPATH variable list of paths separated by ‘:’ in Unix systems and ‘;’ in Windows system.
  • File.pathSeparatorChar ==> pathSeparatorChar: Same as pathSeparator but it’s char.
  • This is the environment independent property.This will be usefull for file upload and downloading etc.



  1. package commonsUtils;
  2. import java.io.File;

  3. public class FileSep{
  4. public static void main(String[] args){
  5.                   String filepath = File.separator + "sample" + File.separatorChar + "seperator";
  6.                   String pathSep = File.pathSeparator + "sample" + File.pathSeparatorChar + "seperator";                                              

  7.                   System.out.println("The path of the file is : " + filepath);
  8.                   System.out.println("The path of the file is  :  " + pathSep);

  9.  System.out.println("File.separator = "+File.separator)
  10.  System.out.println("File.separatorChar = "+File.separatorChar);
  11.  System.out.println("File.pathSeparator = "+File.pathSeparator);
  12.  System.out.println("File.pathSeparatorChar = "+File.pathSeparatorChar);
  13. }
  14. }


 Output :

 For Windows
 -------------
 The path of the file is  :  \sample\seperator
 The path of the file is  :  ;sample;seperator
 File.separator = \
 File.separatorChar = \
 File.pathSeparator = ;
 File.pathSeparatorChar = ;

 For Unix
 ----------
 The path of the file is  :  /sample/seperator
 The path of the file is  :  :sample:seperator
 File.separator = /
 File.separatorChar = /
 File.pathSeparator = :
 File.pathSeparatorChar = :

Remove null values in ArrayList


  • By Using Collections.singleton(null) we can remove the null values in the list.



  1. package commonsUtils;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;

  7. public class RemDupList {
  8.      public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. List<String> arrayList1 = new ArrayList<String>();

  11.    arrayList1.add("A");
  12.    arrayList1.add("A");
  13.    arrayList1.add("B");
  14.    arrayList1.add("B");
  15.    arrayList1.add("B");
  16.    arrayList1.add("C");
  17.    arrayList1.add(null);
  18.    arrayList1.add(null);
  19.    for (String item : arrayList1){
  20.      System.out.println(item);
  21.    }
  22.    HashSet<String> hashSet = new HashSet<String>(arrayList1);

  23.             arrayList1 = new ArrayList<String>(hashSet);
  24.    System.out.println(" =============== ");
  25.     arrayList1 .removeAll(Collections.singleton(null)); //This will remove the null values
  26.     arrayList1 .removeAll(Collections.singleton("A")); //This will remove the value "A" in the entire list.
  27.    for (String item : arrayList1 ){
  28.      System.out.println(item);
  29.    }
  30.     }
  31. }

 Output : 
  Before Removing null values
 A
 A
 B
 B
 B
 C
 null
 null
 =============== 
 After Removing null values
 B
 C

StringEscapeUtils.escapeXml


  • Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
  • Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
  • Note that numerical \\u unicode codes are unescaped to their respective unicode characters. This may change in future releases.

  1.  package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class StringUtility {
  4.  public static void main(String[] args) {
  5.    String xmlString = "<data>";
  6.    System.out.println("After escapeXml::"+StringEscapeUtils.escapeXml(xmlString));
  7.    System.out.println("After unescapeXml::"+StringEscapeUtils.unescapeXml(xmlString));
  8. }
  9.  }

 Output :
 After escapeXml::&lt;data&gt;
 After unescapeXml::<data>

StringEscapeUtils.escapeSql


  •  Escapes the characters in a String to be suitable to pass to an SQL query.



For example,
  • statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" +
StringEscapeUtils.escapeSql("McHale's Navy") + "'");

  • At present, this method only turns single-quotes into doubled single-quotes ("McHale's Navy" => "McHale''s Navy"). It does not handle the cases of percent (%) or underscore (_) for use in LIKE clauses.

  1.  package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class StringUtility {
  4.  public static void main(String[] args) {
  5.    String unescapedSql = "L'OREAL";
  6.    System.out.println("After escapeSql::"+StringEscapeUtils.escapeSql(unescapedSql));
  7.    System.out.println(unescapedSql);
  8. }
  9.  }

Output :

 After escapeSql::L''OREAL
 L'OREAL

StringEscapeUtils.escapeJavaScript


  • Escapes the characters in a String using JavaScript String rules.
  • Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
  • So a tab becomes the characters '\\' and 't'.
  • The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.



  1.  package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class StringUtility {
  4.  public static void main(String[] args) {
  5. String inputString = "What's i\tn a name?";
  6.    System.out.println("After escapeJavaScript::"+StringEscapeUtils.escapeJavaScript(inputString));
  7.    System.out.println("After unescapeJavaScript::"+StringEscapeUtils.unescapeJavaScript(inputString)); 
  8. }
  9.  }



 Output :

 After escapeJavaScript::What\'s i\tn a name?
 After unescapeJavaScript::What's i n a name?

StringEscapeUtils.escapeJava


  • Escapes the characters in a String using Java String rules
  • Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
  • So a tab becomes the characters '\\' and 't'.
  • The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.



  1.  package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class StringUtility {
  4.  public static void main(String[] args) {
  5.    String inputStr = "sample jav\ta escape";
  6.    System.err.println("After escape ::"+StringEscapeUtils.escapeJava(inputStr));
  7.    System.err.println("After unescape ::"+StringEscapeUtils.unescapeJava(inputStr));
  8.    }
  9.  }



 Output :
 After escape ::sample jav\ta escape
 After unescape ::sample jav a escape

How to Unlock XELSYSADM account in OIM


If Xelsysadm account gets locked in OIM ther are 2 ways to unlock it.
  • If you have any other adminstrator account, login using that acount and unlock the user. 
  • Update the usr table to unlock the xelsysadm account using below commands.
  1.       update usr set usr_login_attempts_ctr=0 where usr_login=’XELSYSADM’;
  2.       update usr set usr_locked=0 where usr_login=’XELSYSADM’;
  3.       Commit;

Remove Duplicate in UserDefinied list

we can remove duplicate object in user definied list by overriding equals and hashcode method in java.
first create Java Bean.

Person.java
-------------

  1. package com.utilitySample;

  2. public class Person{

  3. private String name;

  4. public String getName() {
  5. return name;
  6. }

  7. public void setName(String name) {
  8. this.name = name;
  9. }

  10. /**
  11.  * we have to override equals method to avaoid duplicates in userDefined list.
  12.  */
  13. public boolean equals(Object obj) {
  14. boolean result = false;
  15. Person person = (Person) obj;

  16. if (this.getName().equalsIgnoreCase(person.getName())){
  17. result = true;
  18. }
  19. return result;
  20. }

  21. public int hashCode() {
  22. // TODO Auto-generated method stub
  23. return 1;
  24. }

  25. }

Then create your main java class.

RemDupList.java
------------------


  1. package com.utilitySample;

  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Set;

  6. public class RemDupList {

  7. public static void main(String[] args) {
  8.             List<Person> personList = new ArrayList<Person>();
  9.    Person person = new Person();
  10.    person.setName("amazon");
  11.    personList.add(person);
  12.    person = new Person();
  13.    person.setName("amazon");
  14.    personList.add(person);
  15.    person = new Person();
  16.    person.setName("stalin");
  17.    personList.add(person);
  18.    person = new Person();
  19.    person.setName("stalin");
  20.    personList.add(person);
  21.    for (Person pers:personList){
  22.     System.out.println("Before removing duplicates ::"+pers.getName());
  23.    }
  24.    Set<Person> personSet = new HashSet<Person>(personList);
  25.    for (Person pers:personSet){
  26.     System.out.println("After removing duplicates ::"+pers.getName());
  27.    }
  28.    List<Person> personList2 = new ArrayList<Person>();
  29.    personList2.addAll(personSet);
  30.    System.out.println("size ::"+personList2.size());
  31.    for (int i = 0; i < personList2.size(); i++) {
  32. System.out.println("value ::"+personList2.get(i).getName());
  33. }
  34.   }

  35. }

 Output :
 Before removing duplicates ::amazon
 Before removing duplicates ::amazon
 Before removing duplicates ::stalin
 Before removing duplicates ::stalin
 After removing duplicates ::stalin
 After removing duplicates ::amazon
 size ::2
 value ::stalin
 value ::amazon





Remove Duplicates in list


  • We have to create a list of String.
  • Then pass that string top Hashset.(i.e)HashSet will not allow duplicates.
  • So the resultant list is a unique list.
  • The same procedure you have to follow for Interger etc.For user definied list we have to override 
  • hashCode and Equals method.That you can see in my next post.

  1. package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class RemoveDuplicate {
  4.  public static void main(String[] args) {
  5.      List<String> arrayList1 = new ArrayList<String>();
  6.    arrayList1.add("vel");
  7.    arrayList1.add("vel");
  8.    arrayList1.add("dinesh");
  9.    arrayList1.add("dinesh");
  10.    arrayList1.add("dinesh");
  11.    arrayList1.add("prasanth");

  12.    List<String> arrayList2 = new ArrayList<String>(new HashSet<String>(arrayList1));

  13.    for (String item : arrayList2){
  14.        System.out.println(item);
  15.    }      
  16.     }
  17.  }

 Output :
  dinesh
  vel
  prasanth