String CLASS

Language-level Alias - string

String represents a fixed array of characters.

String is a very special class type in Julian. Its assignment behavior is copy-by-value, instead of copy-by-references. This means assigning a string SA to string SB would first create a bit-level copy of the value that SA points to, then let the copy be pointed by SB, discarding whatever SB was previously referring.

String is immutable. The methods exposed by this class are for reading its contents in various ways, and if any manipulating is implied (such as trim), it always mean to create a new string as the result of manipulation. The original string instance always remains unchanged.

String is iterable. This means one can use foreach grammar on a string, or get a character out of string directly:

for (char c : str) {
  ...
}
char c0 = str[0];
str[0] = 'a'; // This will not cause a runtime error, but won't have any effect either.


String supports concatenation operation by '+', which can also be used along with values of other types, as long as at least one operand is string.

Parent Class

Parent Interfaces

All Members


TypeNameSignature
field Clengthpublic const int length
methodatpublic var at(var)
methodatpublic void at(var, var)
methodcomparepublic int compare(var)
methodcontainspublic bool contains(var)
methodendsWithpublic bool endsWith(var)
methodfirstOfpublic int firstOf(var)
method SfromBytespublic static String fromBytes(Byte[])
method SfromBytespublic static String fromBytes(Byte[], String, int, int)
method SfromCharspublic static String fromChars(Char[])
methodgetIteratorpublic System.Util.IIterator getIterator()
methodindexOfpublic int indexOf(var, int)
method SisEmptypublic static bool isEmpty(String)
methodlastIndexOfpublic int lastIndexOf(var, int)
methodreplacepublic String replace(String, String)
methodsizepublic int size()
methodsplitpublic String[] split(var)
methodstartsWithpublic bool startsWith(var)
methodsubstringpublic String substring(int, int)
methodtoBytespublic byte[] toBytes()
methodtoBytespublic byte[] toBytes(String)
methodtoCharspublic char[] toChars()
methodtoLowerpublic String toLower()
methodtoUpperpublic String toUpper()
methodtrimpublic String trim()

Fields


public const int length

The length of this string.


Methods


public var at(var index)

Get the character at specified index.

This is the implementation of getter method on System.Util.IIndexble.

Parameters

  • index An index at which the character is to be retrieved.

Returns

  • The value retrieved.

Throws


public void at(var index, var value)

This method is implemented for System.Util.IIndexble, but due to the immutability of string it will do nothing.

Parameters

  • index An index at which the given value is to be set.
  • value The value to set.

public int compare(var another)

Compare this string against another value, which can be either a string or a char. If the other value is neither string nor char, returns 0.

This method implements System.Util.IComparable.

Parameters

  • another The other value to compare to.

Returns

  • If a negative value, this string is alphabetically less than the other; if positive, larger; if 0, equal or incomparable.

public bool contains(var search)

Check if the string contains the given string or character.

Parameters

  • search The sub-string, or a single chracater, to search within this string. Note this method is special in that it can take two different types.

Returns

  • true if the searched string/character is found.

Throws


public bool endsWith(var suffix)

Check if the string is ended with the given string or character.

Parameters

  • suffix The sub-string, or a single chracater, to match to the end of this string. Note this method is special in that it can take two different types.

Returns

  • true if the given string/character is matched to the ending sequence of this string.

Throws


public int firstOf(var search)

Get the starting index (0-based) of the first occurence of the given string or character within this string.

To search only within a certain scope of this string, call indexOf() instead.

Parameters

  • search The sub-string, or a single chracater, to search with this string. Note this method is special in that it can take two different types.

Returns

  • If a non-negative value, it's the index marking the start of the first occurence of the given string/character; if negative, the given stirng/character doesn't exist.

Throws


public static String fromBytes(byte[] bytes)

Create a string from an array of bytes in ASCII encoding.

Parameters

  • bytes An array of bytes.

Returns

  • A string comprised of the given chars decoded from the byte array.

Throws


public static String fromBytes(byte[] bytes, String charset, int offset, int length)

Create a string from an array of bytes in specified encoding.

Parameters

  • bytes An array of bytes.
  • charset The charset name. Charset names should have been registered with RFC 2278: IANA Charset Registration Procedures.
  • offset The offset in the array to start converting.
  • length The total count of bytes to use.

Returns

  • A string comprised of the given chars.

Throws


public static String fromChars(char[] chars)

Create a string from an array of chars.

Parameters

  • chars An array of chars.

Returns

  • A string comprised of the given chars.

Throws


public IIterator getIterator()

Get an iterator from this string. The iterator produces characters which consist of this string.

This is the implementation of System.Util.IIterable.

Returns

  • A string iterator ready to move on.

public int indexOf(var search, int offset)

Get the starting index (0-based) of the first occurence of the given string or character within this string.

If only checking for the existence within the entire string, consider using contains() instead.

Parameters

  • search The sub-string, or a single chracater, to search within this string from the specified index, from left to right. Note this method is special in that it can take two different types.
  • offset The index on this string from which the forward search will be performed.

Returns

  • If a non-negative value, it's the index marking the start of the first occurence of the given string/character; if negative, the given stirng/character doesn't exist.

Throws


public static bool isEmpty(String str)

Check if the string is null or empty (containing no characters).

Parameters

  • str The string to check.

Returns

  • true if the checked string is null or empty.

public int lastIndexOf(var search, int offset)

Get the starting index (0-based) of the last occurence of the given string or character within this string.

If only checking for the existence within the entire string, consider using contains() instead.

Parameters

  • search The sub-string, or a single chracater, to search within this string from the specified index, from right to left. Note this method is special in that it can take two different types.
  • offset The index on the this string from which the backward search will be performed.

Returns

  • If a non-negative value, it's the index marking the start of the last occurence of the given string/character; if negative, the given stirng/character doesn't exist.

Throws


public String replace(String oldStr, String newStr)

Replace all occurences of a substring with another one.

Parameters

  • oldStr The string to replace.
  • newStr The string to replace with

Returns

  • A string with all occurences of the specified substring replaced with the new substring.

public int size()

Get the length of the string.

This is the implementation of size() method on System.Util.IIndexble. This is equivalent to length field.

Returns

  • The length of this array.

public String[] split(var splitter)

Split the string into multiple substrings at the specified boundary.

Parameters

  • splitter The boundary char or string to split at, which is not included into the resultant substrings.

Returns

  • An array of strings split out of this string.

public bool startsWith(var prefix)

Check if the string is started with the given string or character.

Parameters

  • prefix The sub-string, or a single chracater, to match to the start of this string. Note this method is special in that it can take two different types.

Returns

  • true if the given string/character is matched to the starting sequence of this string.

Throws


public String substring(int start, int end)

Get a substring out of this string.

Parameters

  • start The starting index (inclusive).
  • end The ending index (exclusive).

Returns

  • A substring out of this string.

Throws


public byte[] toBytes()

Convert this string to an array of bytes in ASCII encoding.

Returns

  • A byte array consisting of all the bytes in this string.

public byte[] toBytes(String charset)

Convert this string to an array of bytes using specified charset.

Parameters

  • charset The charset name, which must have been registered with RFC 2278: IANA Charset Registration Procedures

Returns

  • A byte array consisting of all the bytes in this string, encoded with the specified charset.

Throws


public char[] toChars()

Convert this string to a char array.

Returns

  • A char array consisting of all the characters in this string.

public String toLower()

Convert the string to lower case.

Returns

  • A string with same characters, except in lower case.

public String toUpper()

Convert the string to upper case.

Returns

  • A string with same characters, except in upper case.

public String trim()

Trim the start and end of this tring, removing all the leading and trailing blank chracacters.

Blank characters are line feed (\r), carriage return (\n), horizontal tabulation (\t) and space ( ).

Returns

  • A trimmed string.