我是C#的新手,我很确定这个功能可以从根本上改进:
public static Boolean SuffixExists(String strWhole, String sufx)
{
int iLen = sufx.Length;
if (iLen > 0)
{
String s;
s = strWhole.Substring(strWhole.Length - iLen, iLen);
if (sufx != s) return false;
else
s = null;
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我将从foreach循环中调用此函数.如果传递的子字符串作为传递的整个字符串的后缀出现,那么我将希望通过添加前缀来更改此处传递的整个字符串.我对这段代码进行了一些测试,但我知道它非常难看.我正在使用.Net 3.5和Visual Studio 2008.
如果你想NSTextField在屏幕截图(Tweetie.app)上为特定的子串实现突出显示,你会怎么做?:)
Tweetie链接突出显示http://qkpic.com/88c61
谢谢你的帮助!
如何限制为html.encode显示的字符数?
<%= Html.Encode(item.LastName.Substring(1,30))%>
Run Code Online (Sandbox Code Playgroud)
错误:索引和长度必须引用字符串中的位置.
我试图使用TSQL子字符串函数获取Guid字段的第一部分,如下所示
SELECT SUBSTRING(Guid, 1, 8) AS Gu FROM MyTable
Run Code Online (Sandbox Code Playgroud)
但我得到的只是这个错误.
参数数据类型uniqueidentifier对子字符串函数的参数1无效.
那么这里发生了什么?我应该首先将Guid视为纯字符串还是......?
提前致谢!
当有多个特定字符时,如何使用SubString的IndexOf来选择特定字符?这是我的问题.我想采取路径"C:\ Users\Jim\AppData\Local\Temp \"并删除"Temp \"部分.只留下"C:\ Users\Jim\AppData\Local \"我用下面的代码解决了我的问题,但这假设"Temp"文件夹实际上被称为"Temp".有没有更好的办法?谢谢
if (Path.GetTempPath() != null) // Is it there?{
tempDir = Path.GetTempPath(); //Make a string out of it.
int iLastPos = tempDir.LastIndexOf(@"\");
if (Directory.Exists(tempDir) && iLastPos > tempDir.IndexOf(@"\"))
{
// Take the position of the last "/" and subtract 4.
// 4 is the lenghth of the word "temp".
tempDir = tempDir.Substring(0, iLastPos - 4);
}}
Run Code Online (Sandbox Code Playgroud) public class Asterisk
{
public static void main(String[] args)
{
String output="";
int count=1, input;
System.out.println("Input the size of the triangle from 1 to 50:");
input = 5;
for(count=1;count <= input;count++)
{
output += "*";
System.out.println(output);
}
input -= 1;
for(count =input;count <= input;count--)
{
output = output.substring(0,count);
System.out.println(output);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码正确编译,并且也正确运行.但是在输出的底部它会输出一个错误说:
线程"main"中的异常java.lang.StringIndexOutOfBoundsException:
字符串索引超出范围:-1
Run Code Online (Sandbox Code Playgroud)at java.lang.String.substring(String.java:1937) at Asterisk.main(Asterisk.java:18)
谁能解释这种奇怪的行为?谢谢!
我试图创建一个函数来检查PHP中字符串中的子字符串.
public static function stringCheck ($string, $substring) {
$patern = "/".$substring."/";
if (preg_match($substring, string) {
return true;
}
else return false;
}
Run Code Online (Sandbox Code Playgroud)
但如果我输入preg_match中使用的特殊字符(^.[$()|*+?{),它会搞砸搜索.
我试过类似下面的代码但是没有用
$speicalChar = '/(\^|\.|\[|\$|\(|\)|\*|\+|\?|\{|\\)/';
Run Code Online (Sandbox Code Playgroud)
任何人都有解决方案或者替代preg_match.请记住,我也希望能够检查符号.我尝试使用strstr,但有符号问题.
谢谢=]
例如,一个50个字符的片段.问题当然是关闭任何打开的标签.这样做的好方法是什么?或者为了使事情变得更容易,从片段中完全浏览所有HTML内容的好方法是什么?
假设我有一个字符串string abc = "ROOT\abcd".我想得到子串"abcd".
使用abc.substring(4),我只得到"bcd"作为回报.似乎"a"无法识别,因为"\ a"是C#中的特殊字符.我该怎么做才能获得"abcd"的整个子串?
我对这段代码有一些疑问
public static String reverseString( String s )
{
if ( s.length() == 0 )
return "";
String firstChar = s.substring( 0, 1 );
String reverseRest = reverseString( s.substring( 1 ) );
String result = reverseRest + firstChar;
return result;
}
public static void main( String[] args )
{
String a = "The sky's the limit!";
System.out.println( reverseString( a ) );
}
Run Code Online (Sandbox Code Playgroud)
问题1:终止情况究竟如何运作?s.length如何等于0?
问题2:为什么代码需要"firstChar"能够反转字符串?当reverseString接收0的子字符串而不必添加第一个字符时,为什么代码不起作用?