Arduino> Variable> Data Types> String Function>
charAt()
[StringObject Function]
Description
Access a particular character of the String.
Syntax
myString.charAt(n)
Parameters
myString
: a variable of type String
.n
: a variable. Allowed data types: unsigned int
.Returns
The n’th character of the String.
-------------------------------------------------------------------
compareTo()
[StringObject Function]
Description
Compares two Strings, testing whether one comes before or after the other, or whether they’re equal. The strings are compared character by character, using the ASCII values of the characters. That means, for example, that 'a' comes before 'b' but after 'A'. Numbers come before letters.
Syntax
myString.compareTo(myString2)
Parameters
myString
: a variable of type String.myString2
: another variable of type String.Returns
a negative number
: if myString comes before myString2.0
: if String equals myString2.a positive number
: if myString comes after myString2.concat()
[StringObject Function]
Description
Appends the parameter to a String.
Syntax
myString.concat(parameter)
Parameters
myString
: a variable of type String.parameter
: Allowed data types: String, string, char, byte, int, unsigned int, long, unsigned long, float, double, __FlashStringHelper(F() macro).Returns
true
: success.false
: failure (in which case the String is left unchanged).
-------------------------------------------------------------------
c_str()
[StringObject Function]
Description
Converts the contents of a String as a C-style, null-terminated string. Note that this gives direct access to the internal String buffer and should be used with care. In particular, you should never modify the string through the pointer returned. When you modify the String object, or when it is destroyed, any pointer previously returned by c_str() becomes invalid and should not be used any longer.
Syntax
myString.c_str()
Parameters
myString
: a variable of type String.Returns
A pointer to the C-style version of the invoking String.
endsWith()
[StringObject Function]
Description
Tests whether or not a String ends with the characters of another String.
Syntax
myString.endsWith(myString2)
Parameters
myString
: a variable of type String.myString2
: another variable of type String.Returns
true
: if myString ends with the characters of myString2.false
: otherwise.
-------------------------------------------------------------------
equals()
[StringObject Function]
Description
Compares two Strings for equality. The comparison is case-sensitive, meaning the String "hello" is not equal to the String "HELLO".
Syntax
myString.equals(myString2)
Parameters
myString, myString2
: a variable of type String.Returns
true
: if string equals string2.false
: otherwise.equalsIgnoreCase()
[StringObject Function]
Description
Compares two Strings for equality. The comparison is not case-sensitive, meaning the String("hello") is equal to the String("HELLO").
Syntax
myString.equalsIgnoreCase(myString2)
Parameters
myString
: variable of type String.myString2
: variable of type String.Returns
true
: if myString equals myString2 (ignoring case).false
: otherwise.
-------------------------------------------------------------------
getBytes()
[StringObject Function]
Description
Copies the String’s characters to the supplied buffer.
Syntax
myString.getBytes(buf, len)
Parameters
myString
: a variable of type String.buf
: the buffer to copy the characters into. Allowed data types: array of byte`s.`len
: the size of the buffer. Allowed data types: unsigned int.Returns
Nothing
indexOf()
[StringObject Function]
Description
Locates a character or String within another String. By default, searches from the beginning of the String, but can also start from a given index, allowing for the locating of all instances of the character or String.
Syntax
myString.indexOf(val)
myString.indexOf(val, from)
Parameters
myString
: a variable of type String.val
: the value to search for. Allowed data types: char, String.from
: the index to start the search from.Returns
The index of val within the String, or -1 if not found.
-------------------------------------------------------------------
lastIndexOf()
[StringObject Function]
Description
Locates a character or String within another String. By default, searches from the end of the String, but can also work backwards from a given index, allowing for the locating of all instances of the character or String.
Syntax
myString.lastIndexOf(val)
myString.lastIndexOf(val, from)
Parameters
myString
: a variable of type String.val
: the value to search for. Allowed data types: char, String.from
: the index to work backwards from.Returns
The index of val within the String, or -1 if not found.
length()
[StringObject Function]
Description
Returns the length of the String, in characters. (Note that this doesn’t include a trailing null character.)
Syntax
myString.length()
Parameters
myString
: a variable of type String.Returns
The length of the String in characters.
-------------------------------------------------------------------
remove()
[StringObject Function]
Description
Modify in place a String removing chars from the provided index to the end of the String or from the provided index to index plus count.
Syntax
myString.remove(index)
myString.remove(index, count)
Parameters
myString
: a variable of type String.index
: The position at which to start the remove process (zero indexed). Allowed data types: unsigned int.count
: The number of characters to remove. Allowed data types: unsigned int.Returns
Nothing
Example Code
String greeting = "hello";
greeting.remove(2, 2); // greeting now contains "heo"
replace()
[StringObject Function]
Description
The String replace() function allows you to replace all instances of a given character with another character. You can also use replace to replace substrings of a String with a different substring.
Syntax
myString.replace(substring1, substring2)
Parameters
myString
: a variable of type String.substring1
: another variable of type String.substring2
: another variable of type String.Returns
Nothing
-------------------------------------------------------------------
reserve()
[StringObject Function]
Description
The String reserve() function allows you to allocate a buffer in memory for manipulating Strings.
Syntax
myString.reserve(size)
Parameters
myString
: a variable of type String.size
: the number of bytes in memory to save for String manipulation. Allowed data types: unsigned int.Returns
Nothing
Example Code
String myString;
void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
myString.reserve(26);
myString = "i=";
myString += "1234";
myString += ", is that ok?";
// print the String:
Serial.println(myString);
}
void loop() {
// nothing to do here
}
setCharAt()
[StringObject Function]
Description
Sets a character of the String. Has no effect on indices outside the existing length of the String.
Syntax
myString.setCharAt(index, c)
Parameters
myString
: a variable of type String.index
: the index to set the character at.c
: the character to store to the given location.Returns
Nothing
-------------------------------------------------------------------
startsWith()
[StringObject Function]
Description
Tests whether or not a String starts with the characters of another String.
Syntax
myString.startsWith(myString2)
Parameters
myString, myString2
: a variable of type String.Returns
true
: if myString starts with the characters of myString2.false
: otherwisesubstring()
[StringObject Function]
Description
Get a substring of a String. The starting index is inclusive (the corresponding character is included in the substring), but the optional ending index is exclusive (the corresponding character is not included in the substring). If the ending index is omitted, the substring continues to the end of the String.
Syntax
myString.substring(from)
myString.substring(from, to)
Parameters
myString
: a variable of type String.from
: the index to start the substring at.to
(optional): the index to end the substring before.Returns
The substring.
-------------------------------------------------------------------
toCharArray()
[StringObject Function]
Description
Copies the String’s characters to the supplied buffer.
Syntax
myString.toCharArray(buf, len)
Parameters
myString
: a variable of type String.buf
: the buffer to copy the characters into. Allowed data types: array of char`s.`len
: the size of the buffer. Allowed data types: unsigned int.Returns
Nothing
toDouble()
[StringObject Function]
Description
Converts a valid String to a double. The input String should start with a digit. If the String contains non-digit characters, the function will stop performing the conversion. For example, the Strings "123.45", "123", and "123fish" are converted to 123.45, 123.00, and 123.00 respectively. Note that "123.456" is approximated with 123.46. Note too that floats have only 6-7 decimal digits of precision and that longer Strings might be truncated.
Syntax
myString.toDouble()
Parameters
myString
: a variable of type String.Returns
If no valid conversion could be performed because the String doesn't start with a digit, a zero is returned. Data type:
double
.
-------------------------------------------------------------------
toInt()
[StringObject Function]
Description
Converts a valid String to an integer. The input String should start with an integer number. If the String contains non-integer numbers, the function will stop performing the conversion.
Syntax
myString.toInt()
Parameters
myString
: a variable of type String.Returns
If no valid conversion could be performed because the String doesn’t start with a integer number, a zero is returned. Data type:
long
.toFloat()
[StringObject Function]
Description
Converts a valid String to a float. The input String should start with a digit. If the String contains non-digit characters, the function will stop performing the conversion. For example, the Strings "123.45", "123", and "123fish" are converted to 123.45, 123.00, and 123.00 respectively. Note that "123.456" is approximated with 123.46. Note too that floats have only 6-7 decimal digits of precision and that longer Strings might be truncated.
Syntax
myString.toFloat()
Parameters
myString
: a variable of type String.Returns
If no valid conversion could be performed because the String doesn't start with a digit, a zero is returned. Data type:
float
.
-------------------------------------------------------------------
toLowerCase()
[StringObject Function]
Description
Get a lower-case version of a String. As of 1.0,
toLowerCase()
modifies the String in place rather than returning a new one.Syntax
myString.toLowerCase()
Parameters
myString
: a variable of type String.Returns
Nothing
toUpperCase()
[StringObject Function]
Description
Get an upper-case version of a String. As of 1.0,
toUpperCase()
modifies the String in place rather than returning a new one.Syntax
myString.toUpperCase()
Parameters
myString
: a variable of type String.Returns
Nothing
-------------------------------------------------------------------
trim()
[StringObject Function]
Description
Get a version of the String with any leading and trailing whitespace removed. As of 1.0, trim() modifies the String in place rather than returning a new one.
Syntax
myString.trim()
Parameters
myString
: a variable of type String.Returns
Nothing
Post a Comment
Hi Users, if you have any queries then please let me know.