diff --git a/j/string.j b/j/string.j index bacc5edd01e37..c2bf017708564 100644 --- a/j/string.j +++ b/j/string.j @@ -356,6 +356,9 @@ lc(s::String) = TransformedString((c,i)->lc(c), s) ucfirst(s::String) = TransformedString((c,i)->i==1 ? uc(c) : c, s) lcfirst(s::String) = TransformedString((c,i)->i==1 ? lc(c) : c, s) +uppercase = uc +lowercase = lc + ## string map ## function map(f::Function, s::String) @@ -781,6 +784,7 @@ function split(s::String, delims, include_empty::Bool) strs end +split(s::String) = split(s, " ", false) split(s::String, x) = split(s, x, true) split(s::String, x::Char, incl::Bool) = split(s, (x,), incl) @@ -816,6 +820,33 @@ chop(s::String) = s[1:thisind(s,length(s))-1] chomp(s::String) = (i=thisind(s,length(s)); s[i]=='\n' ? s[1:i-1] : s) chomp(s::ByteString) = s.data[end]==0x0a ? s[1:end-1] : s +function lstrip(s::String) + i = start(s) + while !done(s,i) + c, j = next(s,i) + if !iswspace(c) + return s[i:thisind(s,length(s))] + end + i = j + end + "" +end + +function rstrip(s::String) + r = reverse(s) + i = start(r) + while !done(r,i) + c, j = next(r,i) + if !iswspace(c) + return s[1:thisind(s,length(s))-i+1] + end + i = j + end + "" +end + +strip(s::String) = lstrip(rstrip(s)) + ## string to integer functions ## function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer) diff --git a/test/strings.j b/test/strings.j index 9af287dd7630d..b932a8b26f13a 100644 --- a/test/strings.j +++ b/test/strings.j @@ -238,3 +238,6 @@ end @assert int2str(typemin(Int64), 10) == "-9223372036854775808" @assert int2str(typemin(Int16), 10) == "-32768" @assert int2str(typemin(Int8 ), 10) == "-128" + +# string manipulation +@assert strip("\t hi \n") == "hi"