本文整理汇总了C#中System.String.charAt方法的典型用法代码示例。如果您正苦于以下问题:C# String.charAt方法的具体用法?C# String.charAt怎么用?C# String.charAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.charAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: urlEncode
public static String urlEncode(String fullPath)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int len = fullPath.length();
char c;
for (int index = 0; index < len; index++)
{
c = fullPath.charAt(index);
if (c == '^' || c == '_'
|| c == '\\' || c == '-'
|| c == '.'
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9'))
{
sb.Append(c);
}
else
{
sb.Append('%');
sb.Append(String.Format("{0:X2}", (int)c));
}
}
return sb.ToString();
}
示例2: parseVerboseInt
private int parseVerboseInt(String input) {
String loadedString = String.format("%s", input);
boolean negative = false;
int base = 10;
int shift = 3;
int retVal = -1;
if (input.charAt(0) == '-') {
negative = true;
input = input.substring(1);
}
if (input.indexOf("0x") == 0) {
base = 16;
input = input.substring(2);
}
if (input.indexOf("b") == input.length() - 1) {
shift = 0;
input = input.substring(0, input.length() - 1);
}
try {
retVal = Integer.parseInt(input, base);
} catch (Exception e) {
addGeneralError(loadedString);
validComponent = false;
}
if (validComponent) {
retVal <<= shift;
if (negative)
retVal = 0 - retVal;
}
return retVal;
}
示例3: asciiEndsWithIgnoreCase
/**
* Returns whether the given source string ends with the suffix, ignoring
* case and assuming that the strings are ascii encoded.
*
* @param source
* the string to match.
* @param suffix
* the suffix to test.
* @return {@code true} if the source does end with the given suffix, or
* {@code false} if not.
*/
public static bool asciiEndsWithIgnoreCase(String source, String suffix)
{
int length = suffix.length();
if (length > source.length()) {
return false;
}
int offset = source.length() - length;
for (int i = 0; i < length; i++) {
char c1 = source.charAt(i + offset);
char c2 = suffix.charAt(i);
if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
return false;
}
}
return true;
}
示例4: toASCIIUpperCase
public static String toASCIIUpperCase(String s)
{
int len = s.length();
StringBuilder buffer = new StringBuilder(len);
for (int i = 0; i < len; i++)
{
char c = s.charAt(i);
if ('a' <= c && c <= 'z')
{
buffer.append((char)(c - ('a' - 'A')));
}
else
{
buffer.append(c);
}
}
return buffer.toString();
}
示例5: parseNextCharacter
/**
* Parses <code>source</code> until a non-whitespace character is found.
*
* @param source the string to parse
* @param pos input/ouput parsing parameter.
* @return the first non-whitespace character.
*/
public static char parseNextCharacter(String source,
ParsePosition pos) {
int index = pos.getIndex();
int n = source.length();
char ret = 0;
if (index < n) {
char c;
do {
c = source.charAt(index++);
} while (Character.isWhitespace(c) && index < n);
pos.setIndex(index);
if (index < n) {
ret = c;
}
}
return ret;
}
示例6: quoteIllegal
internal const String encoding = "utf-8"; //$NON-NLS-1$
#endregion Fields
#region Methods
/**
* All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
* and legal characters are converted into their hexidecimal value prepended
* by '%'.
* <p>
* For example: '#' -> %23
* Other characters, which are unicode chars that are not US-ASCII, and are
* not ISO Control or are not ISO Space chars, are preserved.
* <p>
* Called from {@code URI.quoteComponent()} (for multiple argument
* constructors)
*
* @param s
* java.lang.String the string to be converted
* @param legal
* java.lang.String the characters allowed to be preserved in the
* string s
* @return java.lang.String the converted string
*/
internal static String quoteIllegal(String s, String legal)
{
//throws UnsupportedEncodingException {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if ((ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| legal.indexOf(ch) > -1
|| (ch > 127 && !java.lang.Character.isSpaceChar(ch) && !java.lang.Character
.isISOControl(ch))) {
buf.append(ch);
} else {
byte[] bytes = new String(new char[] { ch }).getBytes(encoding);
for (int j = 0; j < bytes.Length; j++) {
buf.append('%');
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
buf.append(digits.charAt(bytes[j] & 0xf));
}
}
}
return buf.toString();
}
示例7: countUTFBytes
internal long countUTFBytes(String str)
{
int utfCount = 0, length = str.length();
for (int i = 0; i < length; i++) {
int charValue = str.charAt(i);
if (charValue > 0 && charValue <= 127) {
utfCount++;
} else if (charValue <= 2047) {
utfCount += 2;
} else {
utfCount += 3;
}
}
return utfCount;
}
示例8: upToWithQuotes
protected internal static bool upToWithQuotes(String s, ParsePosition position,
java.lang.StringBuffer buffer, char stop, char start)
{
int index = position.getIndex(), length = s.length(), count = 1;
bool quote = false;
while (index < length) {
char ch = s.charAt(index++);
if (ch == '\'') {
quote = !quote;
}
if (!quote) {
if (ch == stop) {
count--;
}
if (count == 0) {
position.setIndex(index);
return true;
}
if (ch == start) {
count++;
}
}
buffer.append(ch);
}
// text.07=Unmatched braces in the pattern
throw new java.lang.IllegalArgumentException("Unmatched braces in the pattern"); //$NON-NLS-1$
}
示例9: convertPattern
protected internal virtual String convertPattern(String template, String fromChars, String toChars,
bool check)
{
if (!check && fromChars.equals(toChars)) {
return template;
}
bool quote = false;
StringBuilder output = new StringBuilder();
int length = template.length();
for (int i = 0; i < length; i++) {
int index;
char next = template.charAt(i);
if (next == '\'') {
quote = !quote;
}
if (!quote && (index = fromChars.indexOf(next)) != -1) {
output.append(toChars.charAt(index));
} else if (check
&& !quote
&& ((next >= 'a' && next <= 'z') || (next >= 'A' && next <= 'Z'))) {
// text.05=Invalid pattern char {0} in {1}
throw new java.lang.IllegalArgumentException("Invalid pattern char "+next+" in "+ template); //$NON-NLS-1$
} else {
output.append(next);
}
}
if (quote) {
// text.04=Unterminated quote
throw new java.lang.IllegalArgumentException("Unterminated quote"); //$NON-NLS-1$
}
return output.toString();
}
示例10: utf8Count
static int utf8Count(String value)
{
int total = 0;
for (int i = value.length(); --i >= 0;) {
char ch = value.charAt(i);
if (ch < 0x80) {
total++;
} else if (ch < 0x800) {
total += 2;
} else {
total += 3;
}
}
return total;
}
示例11: getInternalField
/*
* Gets private field value by reflection.
*
* @param fieldName the field name to be set @param target the object which
* field to be gotten
*
internal static Object getInternalField(String fieldName, Object target) {
Object value = AccessController
.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
Object result = null;
java.lang.reflect.Field field = null;
try {
field = target.getClass().getDeclaredField(
fieldName);
field.setAccessible(true);
result = field.get(target);
} catch (Exception e1) {
return null;
}
return result;
}
});
return value;
}*/
protected internal static bool upTo(String s, ParsePosition position,
java.lang.StringBuffer buffer, char stop)
{
int index = position.getIndex(), length = s.length();
bool lastQuote = false, quote = false;
while (index < length) {
char ch = s.charAt(index++);
if (ch == '\'') {
if (lastQuote) {
buffer.append('\'');
}
quote = !quote;
lastQuote = true;
} else if (ch == stop && !quote) {
position.setIndex(index);
return true;
} else {
lastQuote = false;
buffer.append(ch);
}
}
position.setIndex(index);
return false;
}
示例12: valueOf
/**
* <p><code>QName</code> derived from parsing the formatted
* <code>String</code>.</p>
*
* <p>If the <code>String</code> is <code>null</code> or does not conform to
* {@link #toString() QName.toString()} formatting, an
* <code>IllegalArgumentException</code> is thrown.</p>
*
* <p><em>The <code>String</code> <strong>MUST</strong> be in the
* form returned by {@link #toString() QName.toString()}.</em></p>
* <p>The commonly accepted way of representing a <code>QName</code>
* as a <code>String</code> was <a href="http://jclark.com/xml/xmlns.htm">defined</a>
* by James Clark. Although this is not a <em>standard</em>
* specification, it is in common use, e.g. {@link javax.xml.transform.Transformer#setParameter(String name, Object value)}.
* This implementation parses a <code>String</code> formatted
* as: "{" + Namespace URI + "}" + local part. If the Namespace
* URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
* local part should be provided.</p>
*
* <p>The prefix value <strong><em>CANNOT</em></strong> be
* represented in the <code>String</code> and will be set to
* {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX
* XMLConstants.DEFAULT_NS_PREFIX}.</p>
*
* <p>This method does not do full validation of the resulting
* <code>QName</code>.
* <p>The Namespace URI is not validated as a
* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
* The local part is not validated as a
* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
* as specified in
* <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>
*
* @param qNameAsString <code>String</code> representation
* of the <code>QName</code>
* @return <code>QName</code> corresponding to the given <code>String</code>
* @see #toString() QName.toString()
*/
public static QName valueOf(String qNameAsString)
{
// null is not valid
if (qNameAsString == null)
{
throw new java.lang.IllegalArgumentException("cannot create QName from \"null\" or \"\" String");
}
// "" local part is valid to preserve compatible behavior with QName 1.0
if (qNameAsString.length() == 0)
{
return new QName(
XMLConstants.NULL_NS_URI,
qNameAsString,
XMLConstants.DEFAULT_NS_PREFIX);
}
// local part only?
if (qNameAsString.charAt(0) != '{')
{
return new QName(
XMLConstants.NULL_NS_URI,
qNameAsString,
XMLConstants.DEFAULT_NS_PREFIX);
}
// Namespace URI improperly specified?
if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}"))
{
throw new java.lang.IllegalArgumentException(
"Namespace URI .equals(XMLConstants.NULL_NS_URI), "
+ ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "
+ "only the local part, "
+ "\"" + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length()) + "\", "
+ "should be provided.");
}
// Namespace URI and local part specified
int endOfNamespaceURI = qNameAsString.indexOf('}');
if (endOfNamespaceURI == -1)
{
throw new java.lang.IllegalArgumentException(
"cannot create QName from \""
+ qNameAsString
+ "\", missing closing \"}\"");
}
return new QName(
qNameAsString.substring(1, endOfNamespaceURI),
qNameAsString.substring(endOfNamespaceURI + 1),
XMLConstants.DEFAULT_NS_PREFIX);
}
示例13: lastIndexOf
/**
* Searches for the index of the specified character. The search for the
* character starts at the specified offset and moves towards the beginning.
*
* @param subString
* the string to find.
* @param start
* the starting offset.
* @return the index of the specified character, -1 if the character isn't
* found.
* @throws NullPointerException
* if {@code subString} is {@code null}.
* @see String#lastIndexOf(String,int)
* @since 1.4
*/
public virtual int lastIndexOf(String subString, int start)
{
int subCount = subString.length();
if (subCount <= count && start >= 0) {
if (subCount > 0) {
if (start > count - subCount) {
start = count - subCount; // count and subCount are both
}
// >= 1
// TODO optimize charAt to direct array access
char firstChar = subString.charAt(0);
while (true) {
int i = start;
bool found = false;
for (; i >= 0; --i) {
if (value[i] == firstChar) {
found = true;
break;
}
}
if (!found) {
return -1;
}
int o1 = i, o2 = 0;
while (++o2 < subCount
&& value[++o1] == subString.charAt(o2)) {
// Intentionally empty
}
if (o2 == subCount) {
return i;
}
start = i - 1;
}
}
return start < count ? start : count;
}
return -1;
}
示例14: encodeOthers
/**
* Other characters, which are Unicode chars that are not US-ASCII, and are
* not ISO Control or are not ISO Space chars are not preserved. They are
* converted into their hexidecimal value prepended by '%'.
* <p>
* For example: Euro currency symbol -> "%E2%82%AC".
* <p>
* Called from URI.toASCIIString()
*
* @param s
* java.lang.String the string to be converted
* @return java.lang.String the converted string
*/
static String encodeOthers(String s)
{
//throws UnsupportedEncodingException {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch <= 127) {
buf.append(ch);
} else {
byte[] bytes = new String(new char[] { ch }).getBytes(encoding);
for (int j = 0; j < bytes.Length; j++) {
buf.append('%');
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
buf.append(digits.charAt(bytes[j] & 0xf));
}
}
}
return buf.toString();
}
示例15: URL
/**
* Creates a new URL instance using the given arguments. The URL uses the
* specified port instead of the default port for the given protocol.
*
* @param protocol
* the protocol of the new URL.
* @param host
* the host name or IP address of the new URL.
* @param port
* the specific port number of the URL. {@code -1} represents the
* default port of the protocol.
* @param file
* the name of the resource.
* @param handler
* the stream handler to be used by this URL.
* @throws MalformedURLException
* if the combination of all arguments do not represent a valid
* URL or the protocol is invalid.
* @throws SecurityException
* if {@code handler} is non-{@code null}, and a security
* manager is installed that disallows user-defined protocol
* handlers.
*/
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler)
{
// throws MalformedURLException {
if (port < -1)
{
throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
}
if (host != null && host.indexOf(":") != -1 && host.charAt(0) != '[')
{ //$NON-NLS-1$
host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
}
if (protocol == null)
{
throw new java.lang.NullPointerException("Unknown protocol: " + "null"); //$NON-NLS-1$ //$NON-NLS-2$
}
this.protocol = protocol;
this.host = host;
this.port = port;
// Set the fields from the arguments. Handle the case where the
// passed in "file" includes both a file and a reference part.
int index = -1;
index = file.indexOf("#", file.lastIndexOf("/")); //$NON-NLS-1$ //$NON-NLS-2$
if (index >= 0)
{
this.file = file.substring(0, index);
refJ = file.substring(index + 1);
}
else
{
this.file = file;
}
fixURL(false);
// Set the stream handler for the URL either to the handler
// argument if it was specified, or to the default for the
// receiver's protocol if the handler was null.
if (handler == null)
{
setupStreamHandler();
if (strmHandler == null)
{
throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
}
}
else
{
java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
if (sm != null)
{
sm.checkPermission(specifyStreamHandlerPermission);
}
strmHandler = handler;
}
}