Today's Question:  What does your personal desk look like?        GIVE A SHOUT

SEARCH KEYWORD -- STRING



  String intern in Java

In Java, == is used to compare whether two objects have the same memory location, equals() is usually used to compare whether two objects have the same time and the same state.  When comparing two strings, normally we just want to know whether they have same content, so we can choose equals(). But since String comparison is so frequently needed, Java has worked on the String class to make == workable when comparing two String literals. The String class maintains a pool of emp...

   JAVA,STRING     2016-04-10 03:35:25

  Three images to understand immutability of String in Java

String is immutable in Java. This means once a String object is created and instantiated, that object cannot be changed. Any operation on the String object will create a new Object instead of operating on the original content. Below are three images to help you understand String immutability in Java. Declare a String String s = "abcd"; s stores the reference to the string content created in the heap. Assign the String reference to another String variable String s2 = s; s2 stores the same ...

   STRING,JAVA     2016-03-11 20:58:28

  C++ : string beginWith and endWith

C++ is an very powerful programming language. It is efficient and flexible. When writing C++ programs, we may often need to process strings and often we need to check whether a string begin with some substring or end with some substring. We can use following functions to ahieve these:     static bool beginWith(const std::string str,const std::string needle){        return (!str.compare(0,needle.length(),needle));    }    ...

   C++,beginWith,endWith,     2012-08-31 06:53:44

  Fuzzy search algorithm in Sublime Text

Fuzzy string searching is the technique of finding strings that match a pattern approximately (rather than exactly. In many editors, we can easily find the Find menu item to do exact matching. While in Sublime Text and some other editors, we can use fuzzy string searching as well if we just want to have an approximate match. There is some algorithm implemented in Sublime Text to implement achieve this. First the search query is split into characters, join them with a regex wildcard, and then run...

   Sublime Text,Fuzzy search     2013-10-14 22:49:38

  C++/CLR int to System::String^

In C++/CLR (for Microsoft), sometimes we need to convert int to System::String type or vice versa. The simple way is :From System::String^ to int int num= int::Parse(str); // System::String^ to int From int to System::String^ System::String^ str = num.ToString(); // int to System::String^For all the other data types, similar ways can be adopted....

   C++,CLR,Microsoft,System::String,Convert,int     2011-12-15 12:47:22

  Get hostname from a URL using JavaScript

Sometimes we may have strings which contain some UR;s and we may want to retrieve the hostname from the URLs for some statistic use. For example, we may have a URL : http://www.example.com/aboutus.html. We may want to retrieve the www.example.com from the URL. How? Use regular expression. Here I give an example using JavaScript. If you want to check whether a string is a URL or not. Refer to Detect URLs in a Block of Text. In JavaScript, we can have a regular expression like var pattern=/(.+:\/\...

   JavaScript,URL,regular expression, Hostname     2012-06-15 09:16:45

  Be careful about printing error as string in GoLang

In GoLang, we can format and produce string using fmt.Printf(), just like C, GoLang also supports format verbs like %s, %d which can be placeholder for different types of values. But please pay attention when printing error as string so that you will not fall into some trap. Let's first take an example code snippet and see what trap we are talking about. package main import "fmt" type A string func (a A) Error() string { return fmt.Sprintf("%s is an error", a) } func main() { a := A("hello...

   STACKOVERFLOW,GOLANG,FMT     2019-01-23 09:17:15

  A strange behavior of printing struct with nested struct in GoLang

Normally when trying to print a struct , we would use %v to show all data of the struct. It will print the default format for each field in the struct. %v the value in a default format when printing structs, the plus flag (%+v) adds field names But recently we observed a strange behavior when printing a struct with nested struct which has a String() string implemented, the %v format prints an 'unexpected' output per our understanding.   Let's see the example snippet first. pa...

   PROGRAMMING,GOLANG     2018-10-29 09:59:49

  Empty slice vs nil slice in GoLang

In Go, there is a type called slice which is built on top of array. It's a very convenient type when we want to handle a group of data. This post will explain a subtle but tricky difference between empty slice and nil slice. A nil slice is a slice has a length and capacity of zero and has no underlying array. The zero value of slice is nil. If a slice is declared like below, it is a nil slice. package main import "fmt" func main() { var a []string fmt.Println(a == nil) } The output will be t...

   GOLANG,JSON,EMPTY SLICE,NIL SLICE     2018-10-18 09:25:21

  + operation on JavaScript objects

In JavaScript, there are two types of values: primitive and object. Primitives consist undefined, null, boolean, number and string. Other values such as array and function are objects. When applying + operation on different type of values, there would be three kinds of type conversion. Primitive conversion Number conversion String conversion There three type conversions have corresponding abstract operations in JavaScript: ToPrimitive(), ToNumber(), ToString(). For number additi...

   JAVASCRIPT,PROGRAMMING     2018-10-12 22:19:12