Skip to content

Instantly share code, notes, and snippets.

@hakanai
Last active May 4, 2017 11:55
Show Gist options
  • Select an option

  • Save hakanai/28159914861f07bca7d18e3ae603516b to your computer and use it in GitHub Desktop.

Select an option

Save hakanai/28159914861f07bca7d18e3ae603516b to your computer and use it in GitHub Desktop.
$ iex
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> String.slice("cafe\u0301", 0..3)
"café"
iex(2)> String.slice("cafe\u0301", 0..2)
"caf"
$ cat > Cafe.java
import java.text.BreakIterator;
import java.util.Locale;
class Cafe {
public static void main(String[] args) {
String str = "cafe\u0301";
System.out.println(str);
System.out.println("Using substring:");
System.out.println(str.substring(0, 4));
System.out.println("Using BreakIterator:");
BreakIterator iterator = BreakIterator.getCharacterInstance(Locale.ROOT);
iterator.setText(str);
int start = iterator.first();
int end = iterator.next(4);
System.out.println(str.substring(start,end));
}
}
$ javac Cafe.java
$ java Cafe
café
Using substring:
cafe
Using BreakIterator:
café
$ python
Python 2.7.11 (default, Oct 18 2016, 15:17:18)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> u'cafe\u0301'[:4]
u'cafe'
>>> import textwrap
>>> textwrap.wrap(u'e\u0301e\u0301e\u0301', 3)
[u'e\u0301e', u'\u0301e\u0301']
$ python3
Python 3.5.1 (default, Feb 22 2016, 11:23:19)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> u'cafe\u0301'[:4]
'cafe'
>>> import textwrap
>>> textwrap.wrap(u'e\u0301e\u0301e\u0301', 3)
['ée', '́é']
$ irb
irb(main):001:0> RUBY_VERSION
=> "2.0.0"
irb(main):002:0> "cafe\u0301"[0..3]
=> "cafe"
irb(main):003:0> "cafe\u0301".chars
=> ["c", "a", "f", "e", "́"]
$ irb
irb(main):001:0> RUBY_VERSION
=> "2.4.0"
irb(main):002:0> "cafe\u0301"[0..3]
=> "cafe"
irb(main):003:0> "cafe\u0301".chars
=> ["c", "a", "f", "e", "́"]
$ cat > cafe.rs
fn main() {
let s = "cafe\u{0301}";
println!("{}", s);
let slice = &s[..4];
println!("{}", slice);
}
$ rustc cafe.rs
$ ./cafe
café
cafe
Welcome to Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41). Type :help for assistance.
1> String("cafe\u{0301}".characters.prefix(4))
$R0: String = "café"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment