本文整理汇总了C#中String.substring方法的典型用法代码示例。如果您正苦于以下问题:C# String.substring方法的具体用法?C# String.substring怎么用?C# String.substring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类String
的用法示例。
在下文中一共展示了String.substring方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: decode
public static Long decode(String nm)
{
int radix = 10;
int index = 0;
boolean negative = false;
Long result;
if (nm.length() == 0)
throw new NumberFormatException("Zero length string");
char firstChar = nm.charAt(0);
// Handle sign, if present
if (firstChar == '-') {
negative = true;
index++;
} else if (firstChar == '+')
index++;
// Handle radix specifier, if present
if (nm.startsWith(new String("0x"), index) || nm.startsWith(new String("0X"), index)) {
index += 2;
radix = 16;
}
else if (nm.startsWith(new String("#"), index)) {
index ++;
radix = 16;
}
else if (nm.startsWith(new String("0"), index) && nm.length() > 1 + index) {
index ++;
radix = 8;
}
if (nm.startsWith(new String("-"), index) || nm.startsWith(new String("+"), index))
throw new NumberFormatException("Sign character in wrong position");
try {
result = Long.valueOf(nm.substring(index), radix);
result = negative ? Long.valueOf(-result.longValue()) : result;
} catch (NumberFormatException) {
// If number is Long.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be
// rethrown.
String constant = negative ? new String("-" + nm.substring(index))
: nm.substring(index);
result = Long.valueOf(constant, radix);
}
return result;
}
示例2: reverseWords
public String reverseWords(String s)
{
if (s == null)
{
return s;
}
int begin = 0;
int end = 0;
while (begin < s.length() && s.charAt(begin) == ' ')
{
begin++;
}
if (begin == s.length())
{
return "";
}
if (s.length() <= 1)
{
return s;
}
StringBuilder result = new StringBuilder("");
while (begin < s.length() && end < s.length())
{
while (begin < s.length() && s.charAt(begin) == ' ')
{
begin++;
}
if (begin == s.length())
{
break;
}
end = begin + 1;
while (end < s.length() && s.charAt(end) != ' ')
{
end++;
}
if (result.length() != 0)
{
result.insert(0, " ");
}
if (end < s.length())
{
result.insert(0, s.substring(begin, end));
}
else
{
result.insert(0, s.substring(begin, s.length()));
break;
}
begin = end + 1;
}
return result.toString();
}
示例3: reverseWords
// A1: going in reverse order, detect each word to append to the new string
public String reverseWords(String a)
{
if (a == null || a.length() == 0) return a;
StringBuilder b = new StringBuilder();
int end = -1;
int start = -1;
int i = a.length()-1;
while (i >= 0)
{
while (i >= 0 && a.charAt(i) == ' ') i--;
end = i;
while (i >= 0 && a.charAt(i) != ' ') i--;
start = i;
if (start < end) {
if (b.length() > 0) b.append(' ');
b.append(a.substring(start+1, end+1));
}
}
return b.toString();
}